removeEventListener在Javascript中不起作用

时间:2018-08-23 13:27:12

标签: javascript vue.js

我有以下代码来侦听和删除事件。但是,该事件不会被删除:

window.addEventListener('mousemove', (event) => {
    this.controlColumnWidth(event, startOffset, column)
})
window.removeEventListener('mouseup', this.controlColumnWidth)

我该如何解决?

1 个答案:

答案 0 :(得分:3)

您在这里混合了两个不同的事件,mousemovemouseup

还需要确保删除与最初注册的相同的(event) => { ... }函数实例:

const handler = event => {
  this.controlColumnWidth(event, startOffset, column);
};

window.addEventListener('mousemove', handler);
window.removeEventListener('mousemove', handler);

// You can also store the handler on `this` if you need to remove
// the event in a different function (such as in the destroyed hook)