我目前正在开发一个使用 VueJS 的项目。这个项目有很多 mixins 和组件,在 HTML @click
中有很多地方定义。
是否可以获取附加了这些事件列表的 DOM 节点列表?或者扩展主事件监听器功能以便我可以向它添加一些额外的逻辑?我已经搜索了几个小时的答案,但仍然找不到任何有用的信息。
谢谢!
最佳答案
我只是想让您知道,经过大量调试后,我找到了一种方法来确定哪些元素附加了 @click
事件。对于那些仍然想知道的人:
function loopThroughVueNodeChildren(children) {
children.forEach(vnode => {
// Is an @click attached to this element?
if (vnode.data && typeof vnode.data.on !== 'undefined' && typeof vnode.data.on.click === 'function') {
console.log('This element has an @click event! I can do whatever i need to do in here :)');
}
if (vnode.children) {
this.addEventListenerOnVueNodes(vnode.children);
}
})
},
loopThroughVueNodeChildren(this._vnode.children);
关于vue.js - 在 vuejs 中扩展事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50818273/