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
Start learning this pattern below
Jump into concepts and practice - no test required
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.
Practice
Solution
Step 1: Understand lazy loading concept
Lazy loading means loading parts of the app only when needed, not all at once.Step 2: Identify benefit in Angular context
Loading modules on demand speeds up the initial app load and reduces bundle size.Final Answer:
To load modules only when the user navigates to their routes, improving startup speed -> Option BQuick Check:
Lazy loading = load on demand [OK]
- Thinking lazy loading loads all modules upfront
- Confusing lazy loading with preloading
- Assuming lazy loading disables routing
Solution
Step 1: Recall Angular lazy loading syntax
Angular uses dynamic import withloadChildrenreturning a promise resolving to the module.Step 2: Match syntax to options
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } uses the correct arrow function with dynamic import and then to get the module class.Final Answer:
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } -> Option AQuick Check:
Dynamic import + loadChildren = { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } [OK]
- Using deprecated string syntax for loadChildren
- Confusing component with module in route config
- Using wrong property name like loadModule
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }Solution
Step 1: Analyze the route config with loadChildren
The route uses lazy loading with a dynamic import function for DashboardModule.Step 2: Understand lazy loading behavior on navigation
The module loads only when the user visits '/dashboard', not before.Final Answer:
The DashboardModule is loaded only when the user navigates to '/dashboard' -> Option DQuick Check:
Lazy loading triggers on route visit [OK]
- Assuming module loads at app start
- Confusing lazy loading with eager loading
- Thinking syntax causes runtime error
{ path: 'profile', loadChildren: './profile/profile.module#ProfileModule' }Solution
Step 1: Check loadChildren syntax
The string syntax with '#' is deprecated in modern Angular versions (17+).Step 2: Identify correct syntax
Modern Angular requires dynamic import with arrow function for lazy loading.Final Answer:
The string syntax for loadChildren is deprecated and causes errors in Angular 17+ -> Option AQuick Check:
Deprecated string syntax = The string syntax for loadChildren is deprecated and causes errors in Angular 17+ [OK]
- Using old string syntax for loadChildren
- Changing path property name incorrectly
- Replacing loadChildren with non-existent loadModule
Solution
Step 1: Understand lazy loading with guards
To prevent loading a module unless authorized, usecanLoadguard with lazy loading.Step 2: Analyze options for correct guard usage
{ path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } usescanLoadwith dynamic import syntax, which is correct for lazy loaded modules.Step 3: Explain why others are incorrect
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] } is eager loading withcanActivate. { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canActivate: [AuthGuard] } usescanActivatewhich guards route activation but not module loading. { path: 'settings', loadChildren: './settings/settings.module#SettingsModule', canLoad: [AuthGuard] } uses deprecated string syntax.Final Answer:
{ path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } -> Option CQuick Check:
Lazy load + canLoad guard = { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } [OK]
- Using canActivate instead of canLoad for lazy loading
- Using deprecated string syntax for loadChildren
- Applying guards on components instead of modules
