I want to share a variable among custom directive's hooks.
Example:
Vue.directive('demo',{
bind: function(el, binding, vnode) {
const index = setInterval(/* ... */) //I have an "index" here
},
unbind: function(el, binding, vnode) {
clearInterval(index) // I want to use "index" here
}
})
How can I pass my value from bind
to unbind
?
P.S. The only workaround in my mind is to modify el
in a way to attach an html-attribute "my-custom-index" in bind
and to read it in unbind
. But it's soooo hacky...
It seems to be a way to go to use html-attributes:
Vue.directive('demo',{
bind: function(el, binding, vnode) {
//save
el.setAttribute('x-my-attr', index);
},
unbind: function(el, binding, vnode) {
//read
const index = el.getAttribute('x-my-attr'); // The value is a string!
}
})