Oma*_*mar 2 typescript angular-routing angular
我试图removeEventListener在我的角度组件中: Javascript removeEventListener 不工作
...
ngOnInit() {
document.addEventListener('visibilitychange', () =>this.handleVisibleState(), true);
}
ngOnDestroy() {
document.removeEventListener('visibilitychange', () => this.handleVisibleState(), true);
}
handleVisibleState() {
let vis = document.visibilityState === 'visible';
this.configsService.update_collab_visible(vis);
}
...
Run Code Online (Sandbox Code Playgroud)
addEventListener即使在上述作品之后ngOnDestroy ()
如何从角度组件中的文档解除可见性状态?
private visibilityChangeCallback: () => void;
ngOnInit() {
this.visibilityChangeCallback = () => this.handleVisibleState();
document.addEventListener('visibilitychange', this.handleVisibleState, true);
}
ngOnDestroy() {
document.removeEventListener('visibilitychange', this.handleVisibleState, true);
}
handleVisibleState() {
let vis = document.visibilityState === 'visible';
console.log(typeof this.configsService); // undefined
this.configsService.update_collab_visible(vis);
}
Run Code Online (Sandbox Code Playgroud)
结果:
错误类型错误:无法读取未定义的属性“update_collab_visible”
存储回调:
private visibilityChangeCallback: () => void;
ngOnInit() {
this.visibilityChangeCallback = () => this.handleVisibleState();
document.addEventListener('visibilitychange', this.visibilityChangeCallback, true);
}
ngOnDestroy() {
document.removeEventListener('visibilitychange', this.visibilityChangeCallback, true);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1868 次 |
| 最近记录: |