Angular应用程序检查标签页是否关闭,但卡在那里。
创始人
2024-10-30 13:30:48
0

在Angular中,通过HostListener监听window的“beforeunload”事件,可以检查页面是否将被卸载或关闭。但是,如果您使用了RxJS的takeUntil操作符来取消该绑定,则可能会导致代码卡在那里。下面是一个简单的解决方法,您可以在destroy中手动释放subscription来解决这个问题。

import { Component, HostListener, OnDestroy } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-root',
  template: `
    

Example component

` }) export class AppComponent implements OnDestroy { private onDestroy$ = new Subject(); private subscription: Subscription; constructor() { this.subscription = this.checkClosed().pipe( takeUntil(this.onDestroy$) ).subscribe(); } @HostListener('window:beforeunload') checkClose(): void { console.log('Window is about to unload'); } private checkClosed() { return new Observable(observer => { setTimeout(() => { observer.next(); observer.complete(); }, 1000); }); } ngOnDestroy() { this.onDestroy$.next(); this.onDestroy$.complete(); this.subscription.unsubscribe(); } }

在这个例子中,我们手动释放了subscription,并且在组件销毁时使用takeUntil释放subscription和Subject。这就可以避免卡住的问题。

相关内容

热门资讯

透视教你!wepoker祈福有... 透视教你!wepoker祈福有用吗(透视)智星德州插件,教程方针(有挂详细)-哔哩哔哩1、上手简单,...
hhpoker免费透视脚本!德... hhpoker免费透视脚本!德普之星辅助器怎么用(透视)器-总是关于存在有挂1、上手简单,内置详细流...
透视必备!竞技联盟破解版最新版... 透视必备!竞技联盟破解版最新版(透视)德州透视插件,教程法门(有挂攻略)-哔哩哔哩1、全新机制【竞技...
佛手大菠萝13道挂哪里!hhp... 佛手大菠萝13道挂哪里!hhpoker透视脚本(透视)app-原来教你真的是有挂1、在佛手大菠萝13...
透视揭露!wepoker养号规... 透视揭露!wepoker养号规律(透视)竞技联盟透视插件,教程指引(讲解有挂)-哔哩哔哩1、进入到竞...
wepoker软件靠谱么!竞技... wepoker软件靠谱么!竞技联盟透视(透视)方法-真是曝光是有挂1、不需要AI权限,帮助你快速的进...
透视揭幕!hhpoker有没有... 透视揭幕!hhpoker有没有透视挂(透视)wepoker到底有没有透视,教程秘籍(有挂透视)-哔哩...
hhpoker德州透视!wep... hhpoker德州透视!wepoker俱乐部辅助(透视)插件-一直详情存在有挂1、在wepoker俱...
透视关于!we-poker有人... 透视关于!we-poker有人玩吗(透视)wepoker怎么破解游戏,教程方案(果真有挂)-哔哩哔哩...
悦扑克脚本!wepoker怎么... 悦扑克脚本!wepoker怎么买辅助(透视)工具-果然曝光真的是有挂1、起透看视 wepoker怎么...