【问题标题】:CanDeactivate fires only once and not alwaysCanDeactivate 只触发一次,并不总是
【发布时间】: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


【解决方案1】:

也将其添加到其他路线

  {
    path: 'signup/:nickname',
    component: SignupComponent
    canDeactivate: [CanDeactivateGuard]
  },
  {
    path: 'signup',
    component: SignupComponent,
    canDeactivate: [CanDeactivateGuard]
  },

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多