📖 Scenario: You are building a simple Angular app that has two sections: Home and About. To make the app faster, you want to load the About section only when the user visits it. This technique is called lazy loading.
🎯 Goal: Create an Angular app with two modules: HomeModule and AboutModule. Set up routing so that the AboutModule is lazy loaded when the user navigates to the /about path.
📋 What You'll Learn
Create a HomeModule with a HomeComponent that shows 'Welcome to Home!'
Create an AboutModule with an AboutComponent that shows 'About Us Information'
Set up the main app routing to load HomeModule eagerly at path '' (empty path)
Set up the main app routing to lazy load AboutModule at path 'about'
Use Angular standalone components and standalone routing where appropriate
💡 Why This Matters
🌍 Real World
Lazy loading modules helps large Angular apps load faster by splitting code into smaller chunks that load only when needed.
💼 Career
Understanding lazy loading and routing is essential for Angular developers to build efficient, scalable web applications.
Progress0 / 4 steps
1
Create HomeModule with HomeComponent
Create a standalone Angular component called HomeComponent that displays the text 'Welcome to Home!'. Then create a standalone Angular module called HomeModule that declares and exports HomeComponent.
Angular
Hint
Use @Component with standalone: true for HomeComponent. Then create a module class called HomeModule that imports and exports HomeComponent.
2
Create AboutModule with AboutComponent
Create a standalone Angular component called AboutComponent that displays the text 'About Us Information'. Then create a standalone Angular module called AboutModule that declares and exports AboutComponent.
Angular
Hint
Similar to HomeComponent, create AboutComponent with standalone true and a simple template. Then create AboutModule that imports and exports AboutComponent.
3
Set up main app routing with lazy loading
Create an Angular routing configuration array called routes. Add a route with path '' that loads HomeComponent eagerly. Add a route with path 'about' that lazy loads AboutModule using dynamic import syntax.
Angular
Hint
Define routes as an array. Use component: HomeComponent for the empty path. Use loadChildren with dynamic import for the 'about' path.
4
Add RouterModule with routes to AppModule
Create an Angular module called AppModule. Import RouterModule.forRoot(routes) and export RouterModule. This sets up the router with the routes you defined.
Angular
Hint
Import RouterModule.forRoot(routes) in AppModule imports array and export RouterModule.
Practice
(1/5)
1. What is the main purpose of lazy loading modules in Angular routing?
easy
A. To preload all modules when the app starts
B. To load modules only when the user navigates to their route
C. To load modules in alphabetical order
D. To load all modules at once for faster navigation
Solution
Step 1: Understand lazy loading concept
Lazy loading means delaying the loading of a module until it is needed, usually when the user navigates to a route that requires it.
Step 2: Identify the purpose in routing
In Angular routing, lazy loading helps improve app speed by loading modules only on demand, not all at once.
Final Answer:
To load modules only when the user navigates to their route -> Option B
Quick Check:
Lazy loading = load on demand [OK]
Hint: Lazy loading means load modules only when needed [OK]
Angular uses dynamic import with a function returning a promise that resolves to the module class.
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 AdminModule. { path: 'admin', loadChildren: './admin/admin.module#AdminModule' } is old deprecated string syntax. Options C and D are incorrect usage.
A. Using string syntax for loadChildren is deprecated and causes errors
B. Missing component property for the route
C. Path should be capitalized as 'Dashboard'
D. loadChildren should be a component, not a string
Solution
Step 1: Check loadChildren syntax
The string syntax with '#ModuleName' is deprecated in Angular and may cause errors in recent versions.
Step 2: Identify correct syntax
Modern Angular requires loadChildren to be a function returning a dynamic import promise, not a string.
Final Answer:
Using string syntax for loadChildren is deprecated and causes errors -> Option A
Quick Check:
Use dynamic import function, not string [OK]
Hint: Avoid string syntax; use dynamic import function [OK]
Common Mistakes:
Using old string syntax for loadChildren
Confusing component and loadChildren properties
Ignoring case sensitivity in paths
5. You want to lazy load a feature module ProfileModule only when the user navigates to /profile. The module has child routes. Which is the best way to configure the main app routing to achieve this and ensure accessibility and performance?
D. { path: 'profile', loadChildren: './profile/profile.module#ProfileModule' }
Solution
Step 1: Identify modern lazy loading syntax
The correct syntax uses dynamic import with an arrow function returning a promise: loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule).
Step 2: Consider best practices for accessibility and performance
For protected features like a user profile, adding canActivate: [AuthGuard] ensures secure access while lazy loading maintains performance.
Step 3: Eliminate incorrect configurations
Using component property fails for modules with child routes. String syntax like './profile/profile.module#ProfileModule' is deprecated.