Lazy loading helps your app load faster by only loading parts when needed. It saves time and data by not loading everything at once.
Module lazy loading preview 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 Angular routing module sets up lazy loading for the 'products' feature. The ProductsModule will only load when the user visits '/products'. The empty path redirects to '/products'.
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { path: 'products', loadChildren: () => import('./products/products.module').then(m => m.ProductsModule) }, { path: '', redirectTo: 'products', pathMatch: 'full' } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {} // Assume ProductsModule is a separate module with its own routes and components.
Lazy loading works best with feature modules that have their own routing.
Make sure the lazy loaded module has its own RouterModule.forChild() setup.
Use browser DevTools Network tab to see when chunks load during navigation.
Lazy loading delays loading modules until needed, speeding up app start.
Use loadChildren with dynamic import syntax in route definitions.
Helps split app into smaller parts for better performance and user experience.
Practice
Solution
Step 1: Understand lazy loading concept
Lazy loading means delaying the loading of parts of the app until they are needed, not all at once.Step 2: Identify the benefit
This reduces the initial load time and speeds up app startup by loading only essential code first.Final Answer:
To load modules only when they are needed, improving startup speed -> Option AQuick Check:
Lazy loading = load on demand [OK]
- Thinking lazy loading loads all modules at start
- Confusing lazy loading with preloading
- Believing lazy loading disables routing
AdminModule located at ./admin/admin.module.ts in Angular routing?Solution
Step 1: Recall Angular lazy loading syntax
Since Angular 8+, lazy loading uses dynamic import syntax withloadChildrenas a function returning a promise.Step 2: Match syntax to options
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } uses the correct arrow function with 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 with arrow function = { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } [OK]
- Using string syntax with # which is deprecated
- Assigning component instead of loadChildren
- Calling loadChildren without import
{ path: 'shop', loadChildren: () => import('./shop/shop.module').then(m => m.ShopModule) }What happens when the user navigates to
/shop for the first time?Solution
Step 1: Understand lazy loading behavior on navigation
With lazy loading, the module is not loaded at app start but only when the route is visited.Step 2: Apply to given route
When user navigates to '/shop', Angular triggers the dynamic import to load ShopModule asynchronously.Final Answer:
The ShopModule is loaded asynchronously only at navigation time -> Option CQuick Check:
Lazy load triggers on first navigation [OK]
- Assuming module loads at app start
- Thinking navigation fails without preloading
- Believing loading blocks UI synchronously
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }What is the main issue with this code in Angular 17+?
Solution
Step 1: Identify deprecated syntax
Angular versions 8+ require dynamic import syntax for lazy loading, not string with #.Step 2: Explain impact
Using string syntax causes build or runtime errors because Angular no longer supports it.Final Answer:
Using string syntax for loadChildren is deprecated and causes errors -> Option DQuick Check:
String syntax deprecated = Using string syntax for loadChildren is deprecated and causes errors [OK]
- Thinking component is required with loadChildren
- Confusing path type with string vs array
- Believing direct import is mandatory for lazy loading
ProfileModule only when the user navigates to /profile. The module file is at ./profile/profile.module.ts. Which of the following route configurations correctly implements this with Angular 17+ standards and ensures accessibility and performance?Solution
Step 1: Use correct dynamic import syntax
Angular 17+ requiresloadChildrenas a function returning a promise with dynamic import and then to access the module class.Step 2: Verify accessibility and performance
Lazy loading delays module load until navigation, improving performance. Using this syntax supports Angular's router and accessibility features.Final Answer:
{ path: 'profile', loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule) } -> Option BQuick Check:
Dynamic import + then + lazy load = { path: 'profile', loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule) } [OK]
- Using deprecated string syntax
- Assigning component instead of loadChildren
- Missing .then() after import
Ready to prove it?
Pass the test and this pattern moves unknown
