1

我是 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;
}

路由.ts

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

0 回答 0