【问题标题】:Is CanLoad guard usable within Electron application?CanLoad 守卫可以在 Electron 应用程序中使用吗?
【发布时间】:2021-02-03 00:24:00
【问题描述】:

我拥有来自 BE 的权限 DTO,它决定了用户可以访问的应用程序部分。 使用 NgRx 选择器我想在 CanLoad 守卫中使用它,但我无法让它工作。

routes.ts

{
  path: 'acquisition',
  canLoad: [AcquisitionGuard],
  loadChildren: () => import('./acquisition/acquisition.module').then(m => m.AcquisitionModule),
  pathMatch: 'full',
},

守卫:

canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
  console.log('canLoad guard'); //never fires
  return this.store.select(fromPermission.canAcess);
}

由于某种原因,这个守卫永远不会触发(尝试使用console.logdebugger)。当我将其更改为canActive 并在“父”路由文件中执行此操作时,它也不会触发。它触发的唯一时间是当我将其更改为 canActive 并将其移动到 'child' routes.file

acquisition.routes.ts

{
  path: 'acquisition',
  canActive: [AcquisitionGuard], //This is the only time I'll get some response from the guard
  component: AcquisitionComponent,
  children: [...],
},

编辑: 看起来这是由这样一个事实引起的,即一旦模块被加载,'CanLoad' 守卫就不会再次被触发。是否有可能 Electron 一次加载所有内容,因此无法调用此守卫?

【问题讨论】:

    标签: angular typescript electron angular-route-guards


    【解决方案1】:

    您可以在模块中将获取路由定义为子模块,并添加 canActivateChild 来处理此模块的所有路由的保护。

    // acquisition.module.ts
    
    const acquisitionRoutes: Routes = [
        {
            path: '',
            canActivate: [AuthGuard],
            canActivateChild: [AuthGuard],
            children: [
               ...
            ]
        },
    ];
    
    
    // app.routing.ts
     {
         path: 'acquisition',
         loadChildren: () => import('./acquisition/acquisition.module').then(m => m.AcquisitionModule),
         pathMatch: 'full',
    };
    

    看看这个答案:Lazy load module with Router Guard with dynamic routes in Angular4

    【讨论】:

    • 尽管我稍微改变了我的问题,但这实际上有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2020-11-20
    • 1970-01-01
    • 2021-06-03
    • 2018-01-20
    相关资源
    最近更新 更多