如何让所有的Mouse 事件 都触发某个函数?

 <svg class="sketch" @mousemove="moveComponent" @mouseup="removeComponent" @mousedown="chooseComponent" @mouseover="displayTool"

上述函数中都要触发某段相同代码A,所以才想是否能将此段代码A绑定到所有Mouse事件。

暂时这样用,请教更好的方案/现成的API

addEventListener(['mousemove', 'mouseup', 'mousedown'], this.$el, this.triggerWatch);

export function addEventListener(events, el, func) {
    events.forEach(function(event) {
        el.addEventListener(event, func, false);
    });
}
阅读 2.2k
2 个回答

1、在所有的mouse事件中都执行代码A
2、所有mouse事件执行同一函数包含代码A,然后在这个函数中根据mouse事件的类型去调用不同的函数
3、...

可以考虑把某段代码封装成函数,在moveComponent,removeComponent,chooseComponent,displayTool 函数中执行。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题