1

There seems to be a problem when using router navigation with lazy loaded modules. This works fine if I remove lazy loading. But here is my setup according to lazy loading modules:

In my app.template I have the main router-outlet defined.

I have the following root app router

export const ROUTES: Routes = [
  {
    path: 'user/:id', loadChildren: './user/profile/profile.module#ProfileModule'
  }
]

When profile module is loaded into the main router-outlet it will display a page with two routing tabs and inside profile.module I have the following child routes defined

const ROUTES: Routes = [
    {
        path: '', component: ProfileComponent,
        children: [
            { path: 'subPath1', loadChildren: '../subModule1/subModule1.module#SubModule1Module' },
            { path: 'subPath2', loadChildren: '../subModule2/subModule2.module#SubModule2Module' },
        ]
    }
];

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

Inside profile.template I have routing tab defined like this

<nav md-tab-nav-bar>
          <a class="tab-label" md-tab-link [routerLink]="'subPath1'" routerLinkActive #rla="routerLinkActive" [active]="rla.isActive">
            Sub path 1
          </a>
          <a class="tab-label" md-tab-link [routerLink]="'subPath2'" routerLinkActive #rla="routerLinkActive" [active]="rla.isActive">
            Sub path 2
          </a>
        </nav>
      <router-outlet></router-outlet>

When I route e.g. to /user/:id/subPath2 I would expect subModule2 to be loaded into the router-outlet of the md-tab-nav-bar but instead it is being loaded into the main application router-outlet. This closes the profile.component and displays only submodule2 instead of displaying it inside the tab.

Any idea why this is not working with lazy loading modules?

1 Answer 1

1

in Angular2 RouterModule

forChild creates a module that contains all the directives and the given routes, but does not include the router service.

forRoot creates a module that contains all the directives, the given routes, and the router service itself.

I think you most use forRoot, because forChild get only child

 RouterModule.forRoot(ROUTES)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, yes this seems to be the problem but I need forChild because I am lazy loading the module. When I removed the lazy loading it worked fine. Do you know how to make this work for lazy loading modules?
I didn't work with lazy but i think you need add component to children { path: 'subPath1', component: ***SomeComponent*** , loadChildren: '../subModule1/subModule1.module#SubModule1Module' }
Thanks alot, you were right adding the component did the trick. I will mark it as the correct answer!
I'm glad that you helped )
Same issue but didn't get it working. Actually I didn;t get what needs to be SomeComponent
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.