我有一个 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.
}
如何使用此模式窗口来获取用户的输入?
谢谢你。