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)
?
Just by this (in your method in "methods"):
:)
Exactly what Kamil said,
But make sure you don't have fragment instances.
Since in Vue 2.0, no solution seems available, a clean solution that I found is to create a
vue-id
attribute, and also set it on the template. Then oncreated
andbeforeDestroy
lifecycle these instances are updated on the global object.Basically:
If you're starting with a DOM element, check for a
__vue__
property on that element. Any Vue View Models (components, VMs created byv-repeat
usage) will have this property.You can use the "Inspect Element" feature in your browsers developer console (at least in Firefox and Chrome) to view the DOM properties.
Hope that helps!
Since v-ref is no longer a directive, but a special attribute, it can also be dynamically defined. This is especially useful in combination with v-for.
For example:
and in Vue component you can use
The proper way to do with would be to use the
v-el
directive to give it a reference. Then you can dothis.$$[reference]
.Update for vue 2
In Vue 2 refs are used for both elements and components: http://vuejs.org/guide/migration.html#v-el-and-v-ref-replaced