Typescript BeforeUnload 事件未触发

Type Script BeforeUnloadEvent not firing

在我的应用程序中,我有一个评论框,我想在其中显示未保存的消息。在 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
}