CanDeactivate仅触发一次,并非总是触发

时间:2018-12-06 14:21:12

标签: angular angular5 angular-guards

当我单击浏览器后退按钮时,我的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]
  },
...

1 个答案:

答案 0 :(得分:0)

也将其添加到其他路线

  {
    path: 'signup/:nickname',
    component: SignupComponent
    canDeactivate: [CanDeactivateGuard]
  },
  {
    path: 'signup',
    component: SignupComponent,
    canDeactivate: [CanDeactivateGuard]
  },
相关问题