Performance: Child routes and nested routing
This affects page load speed and rendering performance by controlling how much of the UI and components load initially versus on demand.
const routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', loadChildren: () => import('./child1/child1.module').then(m => m.Child1Module) }, { path: 'child2', loadChildren: () => import('./child2/child2.module').then(m => m.Child2Module) } ]} ];
const routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ]} ];
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading child routes | More DOM nodes initially | Multiple reflows if many nested components | Higher paint cost due to large initial render | [X] Bad |
| Lazy loading child routes | Fewer DOM nodes initially | Single reflow on navigation to child | Lower paint cost initially, deferred cost on navigation | [OK] Good |
{ path: 'dashboard', component: DashboardComponent, children: [ { path: 'stats', component: StatsComponent } ] }/dashboard/stats?<router-outlet>./dashboard/stats, DashboardComponent loads first, then StatsComponent loads inside its router-outlet.{ path: 'home', component: HomeComponent, children: { path: 'profile', component: ProfileComponent } }/settings loads SettingsComponent and /settings/profile loads ProfileComponent inside it. Which setup is correct?<router-outlet> to display child routes.Pass the test and this pattern moves unknown