如果使用页面,我们有要求提醒用户的位置,如果使用选择不同的菜单应用程序,应该用Yes或Yes显示提醒,如果用户继续进行应该发生重定向,否则应该回到同一页面。我们尝试过使用ngOnDestroy,但应用程序正在重定向到下一个页面而没有显示Alert。Angular 2 - 如果触摸页面,则在离开页面之前提醒用户
我的第一种方法是:
ngOnDestroy()
{
this.touched = this.eventForm.touched;
if (this.touched)
this.display = true;
}
现在我用CanDeactivate
后卫(指this plunker为例)尝试:
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { SidebarComponent } from './shared/sidebar/sidebar.component';
@Injectable()
export class ConfirmDeactivateGuard implements CanDeactivate<SidebarComponent> {
canDeactivate(target: SidebarComponent) {
if (target.hasChanges()) {
return window.confirm('Do you really want to cancel?');
}
return true;
}
}
的可能的复制(https://stackoverflow.com/questions/35922071/warn-user-of- [警告未保存的更改用户离开页面之前] unsaved-changes-before-leaving-page) –
请检查此链接及其下划线的回购 https://rahulrsingh09.github.io/AngularConcepts/#/guard,我已经实施了can decativate警卫。 –