繁体   English   中英

子路由取决于 Angular 8 中的条件

[英]Child routes depended on a condition in Angular 8

如何根据条件在子路由中加载组件? 我的路线:

const mainRoutes: Routes = [
  {
    path: '',
    component: MainComponent,
    canActivate: [AuthGuard],
    children: [
...
      {
        path: 'dashboard',
        component: DashboardComponent,
        children: [
          if(isUser) { // <= this is an example that I want to do
           return {
             path: '',
             component: UserComponent, // if user is login in it should load this component
           }
          } else {
            return {
             path: '',
             component: NotUserComponent, // if no user it should load this component
           }
          }
        ],
      }
}

@NgModule({
  imports: [RouterModule.forChild(mainRoutes)],
  exports: [RouterModule],
})
export class MainRoutingModule {}

第二个问题:可以在路由器模块中注入授权服务并根据用户登录建立路由吗?

我建议您也为该路线使用防护,您可以在该防护中注入身份验证服务。

是否可以在路由器模块中注入授权服务并构建路由取决于用户登录?

是的,你可以在这里看到,但我不能推荐它。

此致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM