Performance: RouterModule configuration
This affects the initial page load speed and navigation responsiveness by controlling route setup and lazy loading.
imports: [RouterModule.forRoot([{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }, { path: 'profile', loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule) }, { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule) }])]imports: [RouterModule.forRoot([{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }, { path: 'profile', component: ProfileComponent }, { path: 'settings', component: SettingsComponent }])]| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading all routes | High (many components loaded upfront) | Multiple reflows during initial load | High paint cost due to large bundle | [X] Bad |
| Lazy loading feature modules | Low (only active route components loaded) | Single reflow per navigation | Lower paint cost, faster initial render | [OK] Good |
RouterModule.forRoot(routes) in an Angular app?const routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];/about?const routes = [
{ path: 'home', component: HomeComponent },
{ path: 'profile' component: ProfileComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}Pass the test and this pattern moves unknown