我想在用户离开我的angular 2应用程序的特定页面之前警告用户未保存的更改.通常我会使用window.onbeforeunload,但这不适用于单页面应用程序.
我发现在角度1中,你可以挂钩$locationChangeStart事件confirm为用户抛出一个盒子,但我还没有看到任何显示如何使角度2工作,或者如果该事件仍然存在.我也见过为ag1提供功能的插件onbeforeunload,但同样,我还没有看到任何方法将它用于ag2.
我希望其他人找到解决这个问题的方法; 任何一种方法都可以用于我的目的.
我有onbeforeunload事件的自定义消息,并且运行良好但我今天注意到它不再显示我的消息了.相反,它显示"您所做的更改可能无法保存"
window.onbeforeunload = function () {
return 'Custom message'
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以让我知道如何解决它?
我有一个确认/取消模式对话框,当用户离开路线时会弹出该对话框.我通过使用带有canDeactivate方法的后卫来做到这一点.但是我希望canDeactivate等到它返回任何东西之前从模态得到响应.
我试图通过返回一个observable来做到这一点,但它不起作用.
canDeactivate(): Observable<boolean> | boolean {
if(this.isFormStarted()) {
this.formService.showExitModal(true);
return this.formService.getModalSelectionObservable();
}
else {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
单击确认时没有发生任何事情,即使我在if块中执行console.log时可以看到observable工作正常
this.formService.getModalSelectionObservable().subscribe(
value => console.log("dialog value: " + value)
);
Run Code Online (Sandbox Code Playgroud)
以下是表单服务的外观.
private modalConfirmation = new Subject<boolean>();
public setModalSelectionObservable(confirmLeave: boolean) {
this.modalConfirmation.next(confirmLeave);
}
public getModalSelectionObservable(): Observable<boolean> {
return this.modalConfirmation.asObservable();
}
Run Code Online (Sandbox Code Playgroud)