我正在收听这样一个deviceorientationabsolute事件(在ngOnInit上):
this.deviceOrientationEvent = this.onDeviceOrientation.bind(this);
window.addEventListener("deviceorientationabsolute", this.deviceOrientationEvent);我不想再在ngOnDestroy上听那个事件了。我试过这个:
window.removeEventListener("deviceorientationabsolute", this.deviceOrientationEvent);但是我仍然可以在控制台上看到它在听这个事件。
我做错了什么?
发布于 2019-08-18 05:32:37
与通过window.addEventListener方法进行处理不同,您可以使用@HostListener处理它,如下所示
@HostListener('window:deviceorientationabsolute', ['$event'])
deviceOrientationAbsoluteEvent(event) { ... }当组件被销毁时,它将被自动移除。
有关更多细节,请参阅:https://stackoverflow.com/a/41032388/9380944答案。
https://stackoverflow.com/questions/57541864
复制相似问题