I have global directive to hide modal when user click out:
Vue.directive('click-outside', {
bind(el, binding, vNode) {
el.clickOutside = event => {
if (!(el == event.target || el.contains(event.target))) {
vNode.context[binding.expression](event);
event.stopPropagation();
}
}
document.body.addEventListener('click', el.clickOutside)
},
unbind(el) {
document.removeEventListener('click', el.clickOutside)
el.clickOutside = null
}
});
Next I have use this directive:
<img src="noti.png" id="1" v-click-outside="onClickOutside('notification')">
<img src="dots.png" id="2" v-click-outside="onClickOutside('profile')">
I have problem because I cant use this same directive on one component. Vue retururn me erro:
vNode.context[binding.expression] is not a function
I found this query, but the answer not help me.
When add console.log
to directive and click for example on first img
directives will be done twice.
How I can detect (in directive) which img
was clicked ?