Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I navigate to lazy loaded module child routes?

I am trying to lazy load module with child routes declared in it's routing configuration file, The lazy module loaded successfully but I can't navigate to it's child routes.

I am using the technique as if it were a eagerly loaded module. I declared path and inside it it's children. Please see 'moduleB' routing file in the Plunker The link button is trying to navigate to the child route and load CmpC. but it fails.

Here is my root module routing decalration:

{ path: 'feature-b', loadChildren:'src/featureB/featureB.module#FeatureBModule' ,  pathMatch: 'full'}

Here is my lazy loaded module routing declaration.

export const moduleBRoutes: Routes = [

  { path: '', component:CmpC , pathMatch: 'full',children:[
      {path: '' , pathMatch: 'full'},
      { path: 'c', component:CmpB , pathMatch: 'full'}
  ]},

];

export const subRouting: ModuleWithProviders = RouterModule.forChild(moduleBRoutes);

trying to navigate to : localhost:999/feature-b succeeded

trying to navigate to : localhost:999/feature-b/c failed

https://plnkr.co/edit/wFlJoDXRmAlMOswmwWFk?p=preview

like image 358
almog Avatar asked Oct 29 '22 18:10

almog


2 Answers

I had the same issue, in my case no error appear but the view doesnt load, I can solved the problem by this way:

  • This is my lazy module
const routes: Routes = [
  {
    path: ''
    },
    children: [
      { path: '', component: AdminComponent },
      { path: 'users', component: AdminUsersComponent },
      { path: 'games', component: AdminGamesComponent },
      { path: '**', redirectTo: '' }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }
  • This is my root module
const routes: Routes = [
  {
    path: 'admin', loadChildren: 'app/admin/admin.module#AdminModule'
  }
];

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

Now the routes:

  • admin/
  • admin/users
  • admin/games

works perfectly

like image 61
silvelo Avatar answered Nov 15 '22 06:11

silvelo


Working DEMO : https://plnkr.co/edit/xAGjXqpmkLbCokvCt9qQ?p=info

Removed unnecessary usage of

pathMatch:'full'

Don't use it everywhere without any reason.

And its now working as expected.

learn more about pathMatch here https://angular.io/docs/ts/latest/guide/router.html#!#redirect, https://angular.io/docs/ts/latest/api/router/index/Routes-type-alias.html

like image 22
Nikhil Shah Avatar answered Nov 15 '22 05:11

Nikhil Shah