【发布时间】:2019-05-08 06:30:18
【问题描述】:
我的CanDeactivateGuard 并不总是在我单击浏览器后退按钮时触发。
我找不到原因。你能看看我的代码,给我建议吗?
后卫:
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<SignupComponent> {
canDeactivate(component: SignupComponent): boolean {
if (!component.canDeactivate()) {
if (confirm("You have unsaved changes! If you leave, your changes will be lost.")) {
return true;
} else {
return false;
}
}
return true;
}
}
SignupComponent 中的方法:
canDeactivate() {
if (this.activeStep == 'step1' || this.activeStep == 'step5') {
return true;
} else {
return false;
}
}
路由:
...
{
path: 'signup/:nickname',
component: SignupComponent
},
{
path: 'signup',
component: SignupComponent,
canDeactivate: [CanDeactivateGuard]
},
...
【问题讨论】:
-
浏览器后退按钮不能很好地与警卫配合使用。您应该使用不同的方法,例如:stackoverflow.com/questions/40381814/…。或者为
onbeforeunload事件添加主机监听器。
标签: angular angular5 angular-guards