I can't understand how to emit event from directive
This is my template where i try to call method :
<tooltip v-click-outside="clickEvent" v-on:emitedEvent="clickEvent"></tooltip>
From v-click-outside="clickEvent" i got:
Property or method "clickEvent" is not defined on the instance but referenced during render.
My code:
export default {
data() {
return {
}
},
methods: {
clickEvent: function () {
console.log('click')
}
},
}
Vue.directive('click-outside', {
bind: function (el, binding, vnode) {
el.event = function (event) {
if (!(el.contains(event.target))) {
vnode.context.$emit('emitedEvent')
}
}
document.addEventListener('click', el.event)
},
unbind: function (el) {
document.removeEventListener('click', el.event)
},
})
I get:
Invalid handler for event "emitedEvent": got undefined
I suspect the problems you're encountering are primarily based on two problems:
Note that using this template:
template:'<button type="button" @click="clickEvent" v-click-outside>test</button>',
template:'<button type="button" @click="clickEvent" v-click-outside>test</button>',
or use the $root object for emitting and listening for events at the same level ( this.$root.$emit not working in Vue )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.