【发布时间】:2017-08-25 20:42:57
【问题描述】:
我有一个 Angular2 应用程序。它的一个组件实现了 CanDeactivate 接口,以确认用户离开页面的导航。下面是路由守卫的实现:
export class DeactivateGuard implements CanDeactivate<MyComponent> {
canDeactivate(component: MyComponent) {
let can = component.canDeactivate();
return can;
}
}
这是组件的代码:
canDeactivate() {
if (confirm("Are you sure you want to navigate away?")) {
return true;
}
else {
return false;
}
}
现在,我想显示一个自定义模式窗口,而不是浏览器的确认框。我用 Telerik 的 Kendo UI 构建了这样的窗口:
<kendo-dialog title="Please confirm" *ngIf="userConfirm">
Are you sure you want to navigate away?
<kendo-dialog-actions>
<button kendoButton class="k-button" (click)="closeConfirm('no')">No</button>
<button kendoButton class="k-button" (click)="closeConfirm('yes')" primary="true">Yes</button>
</kendo-dialog-actions>
</kendo-dialog>
如果我在代码中的任何位置设置 userConfirm = true,则此模式窗口有效,canDeactivate() 方法除外。
canDeactivate() {
this.userConfirm = true; //Does not work. The modal does not show up.
}
如何使用此模式窗口获取用户输入?
谢谢。
【问题讨论】:
-
userConfirm 是一个全局变量。我可以确认模态窗口有效。我可以从代码中的其他地方激活它,但不能在 canDeactivate()
-
这是我对类似问题的解决方案:stackoverflow.com/questions/46433195/…
标签: angular angular-router-guards