简体   繁体   English

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

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

How I can load a component in a child routes depending on a condition?如何根据条件在子路由中加载组件? My routes:我的路线:

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 {}

And the second question: It's possible to inject Authorization Service in router module and build routes depend on user login in?第二个问题:可以在路由器模块中注入授权服务并根据用户登录建立路由吗?

I suggest you use a guard for that route as well, and you can inject the Auth Service in that guard.我建议您也为该路线使用防护,您可以在该防护中注入身份验证服务。

It's possible to inject Authorization Service in router module and build routes depend on user login in?是否可以在路由器模块中注入授权服务并构建路由取决于用户登录?

Yes, you can as you see here , but I cannot recommend it.是的,你可以在这里看到,但我不能推荐它。

Best regards此致

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

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