0
0
Angularframework~10 mins

Lazy loading routes and modules in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lazy loading routes and modules
App starts
User navigates to route
Check if module loaded?
YesRender component
No
Load module asynchronously
Render component from loaded module
User interacts with app
The app waits to load a module until the user navigates to its route, then loads it asynchronously and renders its components.
Execution Sample
Angular
const routes = [
  { path: 'home', component: HomeComponent },
  { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];
Defines routes where 'admin' module is loaded only when user visits '/admin' path.
Execution Table
StepUser ActionModule Loaded?Action TakenResult
1App starts, user at '/'NoLoad root module onlyHomeComponent ready
2User clicks '/home'Home module loadedRender HomeComponentHomeComponent visible
3User clicks '/admin'Admin module not loadedStart loading AdminModule asynchronouslyLoading spinner or blank screen
4AdminModule loadedAdmin module loadedRender AdminComponent from AdminModuleAdminComponent visible
5User navigates awayModules remain loadedNo unload by defaultApp continues running
💡 Lazy loading triggers only when user navigates to the route; modules load once and stay loaded.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
homeModuleLoadedtruetruetruetruetrue
adminModuleLoadedfalsefalseloadingtruetrue
currentComponentHomeComponentHomeComponentloading screenAdminComponentAdminComponent
Key Moments - 3 Insights
Why doesn't the admin module load when the app starts?
Because lazy loading delays loading until the user navigates to the admin route, as shown in step 3 of the execution_table.
What happens if the user navigates back to '/home' after visiting '/admin'?
The home module is already loaded, so the app just renders HomeComponent without reloading modules, as seen in step 2 and step 5.
Does Angular unload lazy loaded modules when navigating away?
No, once loaded, modules stay in memory for faster navigation, as noted in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'adminModuleLoaded' after step 3?
A"true"
B"loading"
C"false"
D"unloaded"
💡 Hint
Check the variable_tracker column 'After Step 3' for 'adminModuleLoaded'.
At which step does the admin module finish loading?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Module Loaded?' column in execution_table for when admin module becomes loaded.
If the user never visits '/admin', what happens to the admin module?
AIt loads at app start
BIt loads after '/home'
CIt never loads
DIt loads when user clicks a button
💡 Hint
Refer to the concept_flow and execution_table steps 1 and 2 about lazy loading triggers.
Concept Snapshot
Lazy loading delays loading a module until its route is visited.
Use loadChildren with dynamic import in route config.
Modules load asynchronously, improving startup speed.
Once loaded, modules stay in memory.
Helps keep initial app bundle small and fast.
Full Transcript
Lazy loading in Angular means the app does not load all modules at startup. Instead, it waits until the user navigates to a route that needs a specific module. At that moment, Angular loads the module asynchronously. For example, the admin module only loads when the user visits '/admin'. This keeps the app fast initially. The execution table shows the app starting with only the root module loaded. When the user clicks '/admin', Angular starts loading the admin module. After loading, it renders the admin components. Modules stay loaded after first load for quick navigation. This technique improves app performance by reducing initial load size.