doorman
doorman

Reputation: 16949

Route depending if user is logged in

I have the following route

{ path: '', loadChildren: './home/home.module#HomeModule' }

When the user is logged in I want to load a different module for the same root path.

What is the proper way to route to a page depending on if a user is logged in.

Using resetconfig seems to be the way to go. But I am not sure how to achieve this with lazy loading. I am calling resetConfig during app load.

 this.router.resetConfig([
     { path: '', loadChildren: './user/user.module#UserModule' },
  ]);

But I probably also need to specify the child roots dynamically in the user.module because If they are already defined in the module file the route will be overwritten.

const ROUTES: Routes = [
 { path: '', component: UserComponent }
]; 

@NgModule({
imports: [
    RouterModule.forChild(ROUTES),
    ],

declarations: [
    UserComponent
],
exports: [UserComponent],
providers: []
 })
 export class UserModule {
 }

Any idea how to achieve resetConfig with lazy loading?

Upvotes: 0

Views: 151

Answers (1)

s-f
s-f

Reputation: 2141

During the runtime you can use Router.resetConfig() method to alter the routes configuration and replace the module name for this specified path

Upvotes: 1

Related Questions