类型脚本BeforeUnloadEvent不触发

时间:2019-02-15 13:41:03

标签: javascript typescript onbeforeunload

在我的应用程序中,我有一个注释框,我想在其中显示一条未保存的消息。在功能warnOpenEditor中,我添加了window.onbeforeunload事件,但不会触发。附加了TS和html文件。

  

要触发此事件,我还需要做其他事情吗?     TS文件

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">

1 个答案:

答案 0 :(得分:1)

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

您应该使用此代码

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