Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

组件的destroyed方法中移除事件的方式是错误的。 #3555

Closed
374632897 opened this issue Mar 17, 2017 · 1 comment
Closed

组件的destroyed方法中移除事件的方式是错误的。 #3555

374632897 opened this issue Mar 17, 2017 · 1 comment

Comments

@374632897
Copy link

374632897 commented Mar 17, 2017

ElementUI version

1.2.4

OS/Browsers version

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36

Vue version

2.0.0

Reproduction Link

http://jsbin.com/qukasapuha/6/edit?js,console,output

Steps to reproduce

DEMO中已有说明

What is Expected?

在组件销毁的时候能够正常移除元素绑定的事件

What is actually happening?

并没有移除。

如下代码
main.vue

  destroyed() {
    const reference = this.reference;
    off(reference, 'mouseup');
    off(reference, 'mousedown');
    off(reference, 'focus');
    off(reference, 'blur');
    off(reference, 'mouseleave');
    off(reference, 'mouseenter');
    off(document, 'click', this.handleDocumentClick);
  }

dom.js

export const off = (function() {
  if (!isServer && document.removeEventListener) {
    return function(element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false);
      }
    };
  } else {
    return function(element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler);
      }
    };
  }
})();

实际上,当调用ele.removeEventListener的时候, 至少需要两个参数, 如果没有传递第二个参数(handler), 将会报错。
image
而在off方法中, 传递了handler, 但是在调用中handler为undefined, 所以使得removeListner方法最后接收到的参数数量是3, 从而避免了报错, 但是事件并没有被移除。当然, 元素已经被销毁了,而即便是事件正常移除, 监听器真正的被移除也是在下一下垃圾回收当中, 那么这个问题也并不是什么问题了。 这里我只是想提出这种方式的不合理之处而已。

@374632897 374632897 changed the title 组件的destroyed方法中关闭事件的方式是错误的。 组件的destroyed方法中移除事件的方式是错误的。 Mar 17, 2017
QingWei-Li added a commit to QingWei-Li/element that referenced this issue Mar 22, 2017
@Leopoldthecoder
Copy link
Contributor

Fixed in #3672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants