Angular 使用AuthGuard的不同角色的多个/仪表板路由

Angular 使用AuthGuard的不同角色的多个/仪表板路由,angular,angular-routing,angular-route-guards,Angular,Angular Routing,Angular Route Guards,我希望我的主页为不同的角色加载不同的模块 const routes: Routes = [ { path: 'login', component: LoginComponent, }, { path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', canLoad: [AuthGuard], canActivate: [AuthGuard], }

我希望我的主页为不同的角色加载不同的模块

const routes: Routes = [
      {
        path: 'login',
        component: LoginComponent,
      },
      { path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', canLoad: [AuthGuard], canActivate: [AuthGuard], },
      {
        path: '',
        loadChildren: './dashboard/dashboard.module#DashboardModule',
        canActivate: [true]
      },
]
这里是AuthGuard

canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    if (localStorage.getItem('ISTRAINER') === Role.Trainer
    && next.routeConfig.loadChildren === './dashboard/dashboard.module#DashboardModule') {
      return true;
    }
    return false;
  }
  canLoad(route: Route): boolean {
    return false;
  }
canActivate(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnapshot
):可观察的|承诺|布尔值{
if(localStorage.getItem('ISTRAINER')==角色.Trainer
&&next.routeConfig.loadChildren==='./仪表板/仪表板.module#仪表板模块'){
返回true;
}
返回false;
}
canLoad(路由:路由):布尔值{
返回false;
}
当canLoad:[AuthGuard]返回false时 路由器没有检查下一个路由

或者有没有办法根据路线来改变孩子们

实际上,我想在登录时实现这一点 如果记录了学生角色,则在路线“仪表板”或“”上加载学生模块 在路线“仪表板”或“培训师模块”上加载(如果培训师角色记录在
如果在路径“仪表板”上或在“管理模块”上加载管理角色,则当路径匹配且angular认为这是您请求的路径时,这是预期的行为。在您的情况下,唯一可能的选择似乎是在角色更改时更新路由配置。请参阅文档。

您可以使用angular的url匹配器

像这样,您可以编写一个匹配器,并在那里检查正确的角色。 如果您仍然想确保模块无法加载,您可以设置防护

我自己也有这个问题,发现这篇文章很有帮助:

您可以根据canActivate函数中的条件从typescript路由到您的路径。添加了更多说明,请查看tooSo,您将在仪表板模块中使用3个仪表板组件?就像学生一样,你有一个学生仪表板,管理员->管理员仪表板,教师->教师仪表板。对于localStorage中的任何内容,您将路由到特定的仪表板?这是用例吗?
import { trainerMatcher } from './trainerMatcher'
import { studentMatcher } from './studentMatcher'
{
 path : '',
 matcher : trainerMatcher,
 loadChildren : 'trainerModule',
 canLoad : [AuthGuard]
},
{
 path : '',
 matcher : studentMatcher,
 loadChildren : 'studentModule'
}