我想知道如何实现退出时确认组件,以便在页面刷新或离开选项卡或窗口或屏幕时我可以执行退出时确认方法,这样如果用户单击OK
,他将离开屏幕并单击NO
他会留在同一个屏幕上吗?
我在这里使用的代码是:
import { Component, OnInit, Input, Output, HostListener } from '@angular/core';
@Component({
selector: 'confirmonexit',
templateUrl: './confirm-on-exit.html'
})
export class ConfirmOnExitComponent {
@Input() isDirty: boolean;
public isConfirmed: boolean = false;
@HostListener('window:beforeunload',['$event']) beforeUnloadHander() {
if (this.isDirty) {
let msg: string = 'Are you sure you want to navigate without saving the entered data on the screen ? '
if (confirm(msg)) {
this.isDirty = false;
this.isConfirmed = false;
} else {
this.isDirty = true;
event.preventDefault();
}
}
}
}
appModule added - this component in declarations
html added = <confirmonexit [isDirty]="CreateEditForm.form.dirty"></confirmonexit>
我不知道错误在哪里。我无法执行此功能。谁能帮帮我?