在Angular 15中,我为bepreunload事件添加了一个事件侦听器,如果用户当前正在播放音频文件,我会显示通常的对话框.
我找不到一种方法来捕获用户是否单击了Cancel.如果音频正在播放,如果他们取消了,我希望它继续播放.
这样做的原因是我需要知道用户是否听到了整个音频剪辑.
我的代码是在页面init上运行的函数handleUnexpectedPageClose
handleUnexpectedPageClose(): void {
const pageCloseSub = fromEvent(window, 'beforeunload').subscribe((event: BeforeUnloadEvent) => {
event.preventDefault();
// Doing this now as we dont know if the user cancels the close
this.endAudio();
return (event.returnValue = '');
});
this.masterSubscription.add(pageCloseSub);
}
我在页面上使用master Subcription对销毁进行清理.