Lazy loading helps your app load faster by only loading parts when needed. It keeps the app small at start and loads extra features later.
Lazy loading routes and modules in Angular
const routes: Routes = [ { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) } ];
The loadChildren property uses a function that returns a dynamic import.
This syntax tells Angular to load the module only when the user navigates to the 'feature' path.
const routes: Routes = [ { path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) } ];
const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) } ];
This routing setup lazy loads the ProductsModule when the user navigates to '/products'. The empty path redirects to '/products'. This keeps the initial app bundle small and loads product features only when needed.
import { Routes } from '@angular/router'; export const routes: Routes = [ { path: 'products', loadChildren: () => import('./products/products.module').then(m => m.ProductsModule) }, { path: '', redirectTo: 'products', pathMatch: 'full' } ];
Lazy loading improves app speed but requires modules to have their own routing setup.
Use Angular DevTools or browser network tab to see lazy loaded chunks being fetched.
Always test lazy loaded routes to ensure modules load correctly without errors.
Lazy loading delays loading modules until needed, speeding up app start.
Use loadChildren with dynamic import syntax in route definitions.
Helps manage large apps by splitting code into smaller, on-demand pieces.