简体   繁体   中英

How do i configure the app-routing.module in Angular 5 to load one or the other of two child routing modules, based on a condition?

I have the following application:

├── app.component.html
├── app.component.ts
├── app.module.ts
├── app-routing.module.ts
├── modules
│   ├── home
│   │   ├── components
│   │   ├── home.module.ts
│   │   └── home-routing.module.ts
│   └── user
│       ├── components
│       ├── services
│       │   └── auth-guard.service.ts
│       ├── user.module.ts
│       └── user-routing.module.ts
└── shared
    ├── components
    │   └── navbar
    │       ├── navbar.component.html
    │       ├── navbar.component.scss
    │       └── navbar.component.ts
    ├── services
    │   └── auth.service.ts
    └── shared.module.ts

The app-routing.module is just basic so far as this is the beginning of the application

app-routing.module.ts

const routes: Routes = [
    { path: '**', redirectTo: "''", pathMatch: 'full' },
    { path: '' /*...*/ }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

My question is how do i configure the application, so that the route '' in app-routing.module points to the route '' in home-routing.module based on a condition defined in auth.service, and otherwise to the route '' defined in user-routing.module? The modules home and user each have their respective child routes.

The logic behind my question is that i wish to have two separate modules, one for the part of the app that users can access when not authenticated, and one for authenticated users. Once a user is authenticated, i want the default route to point to the related module and its routing module, based on the authentication logic defined in auth.service.

I realize that i can use canActivate to protect the user module, however i cannot find any resources that display how i can accomplish this conditional kind of routing logic.

Thanks in advance

No can do. You should use canActivate and redirect to different routes based on auth state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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