简体   繁体   English

使用打字稿,RemoveEventListener在Angular2中不起作用

[英]RemoveEventListener is not working in Angular2 using typescript

I'm using three.js in Angular2. 我在Angular2中使用three.js。 The code is simple, as following. 代码很简单,如下所示。

this.webGLRenderer.domElement.addEventListener('mousedown', ()=>this.onMouseDown(<MouseEvent>event), false);

this.webGLRenderer.domElement.removeEventListener('mousedown', ()=>this.onMouseDown(<MouseEvent>event), false);

I also tried this 我也尝试过

this.webGLRenderer.domElement.removeEventListener('mousedown', this.onMouseDown, false);

I still got the event on the domelement anyway. 无论如何,我仍然在参加比赛。 It seems like, addEventListener is working properly, but can not remove it. 看来,addEventListener工作正常,但无法将其删除。 Thanks in advance. 提前致谢。

to use removeEventListerner, the functions callbacks can not be anonymous, so your second example should work, as long as you are also using this.onMouseDown when adding the listener. 要使用removeEventListerner,函数回调不能是匿名的,因此,只要添加侦听器时也使用this.onMouseDown,第二个示例就可以工作。

To remove event handlers, the function specified with the addEventListener() method must be an external function. 要删除事件处理程序,用addEventListener()方法指定的函数必须是外部函数。

from HERE 这里

try: 尝试:

this.webGLRenderer.domElement.addEventListener('mousedown', this.onMouseDown, false);
this.webGLRenderer.domElement.removeEventListener('mousedown', this.onMouseDown, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM