How can I find the vue.js component corresponding to a DOM element?
If I have
element = document.getElementById(id);
Is there a vue method equivalent to the jQuery
$(element)
?
How can I find the vue.js component corresponding to a DOM element?
If I have
element = document.getElementById(id);
Is there a vue method equivalent to the jQuery
$(element)
?
So I figured
$0.__vue__
doesn't work very well with HOCs (high order components).From the template above, if you have
ListItem
component, that hasProductItem
as it's root, and you try$0.__vue__
in console the result unexpectedly would be theListItem
instance.Here I got a solution to select the lowest level component (
ProductItem
in this case).Plugin
Install
Use
$0.__vueComponent__
.Bonus feature
If you want more, you can just use
$0.__vue__.$parent
. Meaning if 3 components share the same dom node, you'll have to write$0.__vue__.$parent.$parent
to get the main component. This approach is less laconic, but gives better control.In Vue.js 2 Inside a Vue Instance or Component:
this.$el
to get the HTMLElement the instance/component was mounted toFrom an
HTMLElement
:.__vue__
from the HTMLElementvar vueInstance = document.getElementById('app').__vue__;
Having a
VNode
in a variable calledvnode
you can:vnode.elm
to get the element that VNode was rendered tovnode.context
to get the Vue instance that VNode belogs tovnode.componentInstance
to get the Component instance that VNode is aboutSource, literally: vue/flow/vnode.js.
Runnable Demo:
If you want listen an event (i.e OnClick) on an input with "demo" id, you can use: