Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Lazy Loading Routes and Modules in Angular
📖 Scenario: You are building a simple Angular app with 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 routes: '' for Home and 'about' for About. The About module should be loaded lazily when the user navigates to /about.
📋 What You'll Learn
Create a HomeComponent for the home page
Create an AboutModule with AboutComponent inside
Set up the main routing module with a route for HomeComponent
Configure lazy loading for the AboutModule using Angular routing
💡 Why This Matters
🌍 Real World
Lazy loading helps large Angular apps load faster by loading parts only when needed, improving user experience.
💼 Career
Understanding lazy loading is important for Angular developers to optimize app performance and manage code splitting.
Progress0 / 4 steps
1
Create HomeComponent and AboutModule
Create a component called HomeComponent with a simple template showing 'Welcome to Home'. Also create a module called AboutModule that declares a component called AboutComponent with a template showing 'About Us'.
Angular
Hint
Use @Component decorator to create components and @NgModule to create the AboutModule.
2
Set up AppRoutingModule with Home route
Create a routing module called AppRoutingModule. Import RouterModule and set up a route with path '' that loads HomeComponent. Export RouterModule configured with this route.
Angular
Hint
Use RouterModule.forRoot() with routes array and export the routing module.
3
Add lazy loading route for AboutModule
Add a route to the routes array with path 'about' that lazy loads AboutModule using loadChildren with a dynamic import: () => import('./about.module').then(m => m.AboutModule).
Angular
Hint
Use the loadChildren property with a dynamic import function to lazy load the AboutModule.
4
Import AppRoutingModule in AppModule
Create an AppModule that imports AppRoutingModule and declares HomeComponent. Also bootstrap HomeComponent.
Angular
Hint
Use @NgModule to declare HomeComponent, import BrowserModule and AppRoutingModule, and bootstrap HomeComponent.
Practice
(1/5)
1. What is the main purpose of lazy loading modules in Angular?
easy
A. To preload all modules at application start for faster navigation
B. To load modules only when the user navigates to their routes, improving startup speed
C. To bundle all modules into a single large file
D. To disable routing in the application
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 B
Quick Check:
Lazy loading = load on demand [OK]
Hint: Lazy loading delays module load until route is visited [OK]
Common Mistakes:
Thinking lazy loading loads all modules upfront
Confusing lazy loading with preloading
Assuming lazy loading disables routing
2. Which of the following is the correct syntax to lazy load a module in Angular routing?
B. { path: 'admin', loadChildren: './admin/admin.module#AdminModule' }
C. { path: 'admin', component: AdminModule }
D. { path: 'admin', loadModule: () => import('./admin/admin.module') }
Solution
Step 1: Recall Angular lazy loading syntax
Angular uses dynamic import with loadChildren returning 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.
A. The string syntax for loadChildren is deprecated and causes errors in Angular 17+
B. The path property should be named routePath
C. The module path should not include './' prefix
D. loadChildren should be replaced with loadModule
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 A
Quick Check:
Deprecated string syntax = The string syntax for loadChildren is deprecated and causes errors in Angular 17+ [OK]
Hint: Use dynamic import syntax, not string with # [OK]
Common Mistakes:
Using old string syntax for loadChildren
Changing path property name incorrectly
Replacing loadChildren with non-existent loadModule
5. You want to lazy load a feature module only if the user is authenticated. Which approach correctly combines lazy loading with route guarding in Angular?