[英]In Angular 2, How can I remove click event from a component / directive, if “click” was attached directly in an HTML tag?
[英]How i can emit event from directive by click outside of element?
我不明白如何從指令中發出事件
這是我嘗試調用方法的模板:
<tooltip v-click-outside="clickEvent" v-on:emitedEvent="clickEvent"></tooltip>
從 v-click-outside="clickEvent" 我得到:
Property or method "clickEvent" is not defined on the instance but referenced during render.
我的代碼:
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)
},
})
我得到:
Invalid handler for event "emitedEvent": got undefined
我懷疑您遇到的問題主要基於兩個問題:
請注意,使用此模板:
template:'<button type="button" @click="clickEvent" v-click-outside>test</button>',
template:'<button type="button" @click="clickEvent" v-click-outside>test</button>',
或使用 $root 對象在同一級別發出和偵聽事件( this.$root.$emit 在 Vue 中不起作用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.