0
0
Angularframework~10 mins

Module lazy loading preview in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Module lazy loading preview
App starts
User navigates to route
Check if module is loaded
Load module asynchronously
Render module components
User interacts with module
App continues
This flow shows how Angular 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: Routes = [
  { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) },
  { path: '', component: HomeComponent }
];
Defines routes where the 'admin' module is loaded only when the user visits '/admin', saving initial load time.
Execution Table
StepUser ActionModule Loaded?Action TakenResult
1App starts, user at '/'NoLoad HomeComponent immediatelyHome page shown
2User clicks link to '/admin'NoStart loading AdminModule asynchronouslyLoading spinner or blank until ready
3AdminModule finishes loadingYesRender AdminModule componentsAdmin page shown
4User navigates within AdminModule routesYesReuse loaded AdminModuleFast navigation inside admin
5User navigates away from '/admin'YesKeep AdminModule loadedNo reload needed if return
6User refreshes browser at '/admin'NoLoad AdminModule againAdmin page shown after load
💡 Execution stops when user leaves app or closes browser; modules remain loaded during session.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
AdminModuleLoadedfalsetruetruetruetrue
CurrentRoute'' (home)'/admin''/admin''/home''/home'
Key Moments - 3 Insights
Why doesn't Angular load the AdminModule at app start?
Because the route uses loadChildren with a function, Angular waits until the user navigates to '/admin' before loading the module, as shown in execution_table step 2.
What happens if the user navigates back to '/admin' after leaving it?
Since AdminModule is already loaded (execution_table step 5), Angular reuses it without reloading, making navigation faster.
Does Angular unload modules when the user leaves their routes?
No, modules stay loaded during the app session to avoid reloading delays, as seen in variable_tracker where AdminModuleLoaded remains true after step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Angular start loading the AdminModule?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Check the 'Action Taken' column for when loading starts.
According to variable_tracker, what is the value of AdminModuleLoaded after step 3?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Look at the AdminModuleLoaded row under 'After Step 3' column.
If the user refreshes the browser at '/admin', what happens according to the execution table?
AHomeComponent loads instead
BAdminModule is already loaded and reused
CAdminModule loads again asynchronously
DApp crashes
💡 Hint
See step 6 in the execution table for refresh behavior.
Concept Snapshot
Angular lazy loading delays loading a module until its route is visited.
Use loadChildren with a dynamic import function in route config.
This improves app startup speed by loading less code initially.
Once loaded, the module stays in memory for fast reuse.
Refreshing at a lazy route reloads the module asynchronously.
Full Transcript
This visual execution shows how Angular lazy loads modules. When the app starts, only the home component loads. When the user clicks the admin link, Angular checks if the AdminModule is loaded. If not, it loads it asynchronously. After loading, it renders the admin components. The module stays loaded for quick navigation inside admin routes. If the user leaves and returns, Angular reuses the loaded module. Refreshing the browser at '/admin' triggers loading the module again. Variables track if the module is loaded and current route. This approach speeds up app start by loading modules only when needed.