0

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?

doorman
  • 15,707
  • 22
  • 80
  • 145
  • 2
    https://angular.io/docs/ts/latest/guide/router.html#!#guards – eko Feb 15 '17 at 10:28
  • Thanks @echonax, however I want to re-route the user to the same path but load different module. E.g. the root path www.examplesite.com should show different moduels depending on if the user is logged in. Do you know if this is possible with routes? – doorman Feb 15 '17 at 10:40
  • http://stackoverflow.com/questions/40293240/how-to-manually-lazy-load-a-module – eko Feb 15 '17 at 10:42
  • @doorman: You're saying just redirecting the user to another path (if they're logged in) is not an option? – AngularChef Feb 15 '17 at 10:47
  • Hi @AngularFrance, I don't want the url to change to www.examplesite.com/redirectedroute I would like it to stay www.examplesite.com even though the user is logged in – doorman Feb 15 '17 at 10:51
  • I see. That's an interesting use case. The link that s-f provided in his answer seems like a potential solution. – AngularChef Feb 15 '17 at 10:57

1 Answers1

1

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

s-f
  • 2,091
  • 22
  • 28
  • Hi @s-f resetconfig seems to be the way to go. I updated my question above regarding how to achieve this with lazy loading. – doorman Feb 15 '17 at 11:46
  • @doorman I didn't get this "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." Why you have to define them dinamically and what's the problem to rewrite them? – s-f Feb 15 '17 at 12:45
  • Sorry, about that. In the end I decided to have a different route for each. But I marked this as the correct answer only need to figure out how to make it work for lazy loading. But I don't need it anymore though. – doorman Feb 15 '17 at 12:47
  • 1
    @doorman if you make different routes then you don't need .resetConfig(), just use route guard as described in documentation – s-f Feb 16 '17 at 10:55