CanDeactivate Route Guard即使单击取消按钮,也会将所选路由显示为活动状态

时间:2018-10-14 16:32:10

标签: angular angular-router-guards

我是Angular的新手,我也在互联网上搜索了该问题,但找不到解决方案。谁能帮帮我吗?这对我来说很重要。

我实现了一个可以停用的路由防护,当表单为dirty时,当我们导航到其他菜单时会触发该防护。

问题是当我在客户端菜单上并使表单变脏并导航到激活菜单时,会出现一个弹出窗口,需要我确认离开页面(按预期方式工作)。但是,当我单击“取消”按钮(希望保留在表单页面(客户端)上)时,它仍然使我留在页面上,但是激活选项卡显示它处于活动状态。

@Injectable()
export class CanDeactivateGuard implements CanDeactivate < CanComponentDeactivate > {
  canDeactivate(
    component: CanComponentDeactivate, 
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot
  ): Observable < boolean > | Promise < boolean > | boolean {
    const url: string = currentState.url;
    console.log('currentState: ' + currentState + '\n component.canDeactivate' + component.canDeactivate);
    return component.canDeactivate ? component.canDeactivate() : true;
  }
}

canDeactivate组件中的方法

canDeactivate(): Observable < boolean > | boolean {
  if (this.clientForm.dirty) {
    return this.dialogService.confirm('Do you want to Discard changes?');
  }
  return true;
}

routing.ts

{
  path: 'configuration',
  component: AdminstratorComponent,
  children: [{
      path: 'client',
      component: ClientComponent,
      outlet: 'configuration',
      canDeactivate: [CanDeactivateGuard]
    },
    {
      path: 'activation',
      component: ActivationComponent,
      outlet: 'configuration',
      canDeactivate: [CanDeactivateGuard]
    }
  ]
}

0 个答案:

没有答案