gpt4 book ai didi

javascript - Typescript BeforeUnload 事件未触发

转载 作者:行者123 更新时间:2023-12-03 00:11:24 30 4
gpt4 key购买 nike

在我的应用程序中,我有一个评论框,我想在其中显示一条消息,表明它尚未保存。在函数 warnOpenEditor 中,我添加了 window.onbeforeunload 事件,但它不会触发。附上TS和html文件。

Is there anything else I need to do for this event to fire? TS file

import 
@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
if (this.IsTouched = true) {
event.returnValue = 'You have unfinished changes!';
}
}
private cancel() {
this.IsTouched = false;
if (this.comments.length === 0) {
this.hideComments();
} else {this.hideEditor();}
this.commentText = "";}
private warnOpenEditor() {
this.IsTouched = true;
for (let i = 0; i < 2; i++) {
this.$commentForm.fadeTo(100, 0.3).fadeTo(100, 1.0);}
}
}

SideCommentsComponet.html

                <p class="comment" [innerHTML]="comment.Content | newline">

最佳答案

您是否尝试将卸载事件移出方法?

您应该使用此代码

@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
//check your comment box value to show message
}

关于javascript - Typescript BeforeUnload 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54710608/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com