请关闭关闭标签/窗口或离开页面
时如何保留对话框export class AppComponent{constructor(private confirmationService:ConfirmationService){}@HostListener('window:beforeunload', ['$event']) public onWindowScroll( $event:any){ console.log('scrolled');
return new Promise((observer) => {this.confirmationService.confirm({
message:"All the information would be lost, are you sure you want to move out of the screen?", accept: () => { $event.returnValue ="";
observer(true);
},
reject: () => {
observer(false);
}
});
});
}
}
答案 0 :(得分:0)
ngOnInit() {
const unloadEvent = (e) => {
if (**YOUR CHECK FLAG**) {
const confirmationMessage =
'Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?';
(e || window.event).returnValue = confirmationMessage; // Gecko + IE
return confirmationMessage; // Webkit, Safari, Chrome etc.
}
};
window.addEventListener('beforeunload', unloadEvent);
window.onunload = () => {
// Post Unload operations
}
});
};
}