【发布时间】:2017-07-19 14:54:12
【问题描述】:
我们有要求,如果页面被触摸,我们必须提醒用户,如果使用选择不同的菜单应用程序,如果用户继续继续,则应该显示是或否的警报,那么只有重定向应该发生,否则它应该回退到同一页面。我们已经尝试使用 ngOnDestroy,但是应用程序正在重定向到下一页而不显示警报。
我的第一个方法是:
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;
}
}
【问题讨论】:
-
请检查此链接及其下划线回购rahulrsingh09.github.io/AngularConcepts/#/guard,我已经实现了can decativate guard。
标签: angular