Lazy loading standalone components helps your app load faster by only loading parts when needed. This saves data and improves user experience.
Lazy loading standalone components in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
import { Component } from '@angular/core'; import { RouterModule } from '@angular/router'; @Component({ standalone: true, selector: 'app-lazy', template: `<h1>Lazy Loaded Component</h1>`, imports: [RouterModule] }) export class LazyComponent {} // In your routing module const routes = [ { path: 'lazy', loadComponent: () => import('./lazy.component').then(m => m.LazyComponent) } ];
Use standalone: true to make a component standalone.
Use loadComponent in routes to lazy load the standalone component.
Examples
ProfileComponent only when the user navigates to '/profile'.Angular
const routes = [ { path: 'profile', loadComponent: () => import('./profile.component').then(m => m.ProfileComponent) } ];
Angular
import { Component } from '@angular/core'; @Component({ standalone: true, selector: 'app-dashboard', template: `<p>Dashboard loaded lazily!</p>` }) export class DashboardComponent {}
Sample Program
This example shows a main app component with a link. When you click the link, the LazyComponent loads only then. This saves loading time at the start.
Angular
import { Component } from '@angular/core'; import { provideRouter, RouterOutlet, RouterLink } from '@angular/router'; @Component({ standalone: true, selector: 'app-root', template: ` <h1>Welcome to Lazy Loading Demo</h1> <nav> <a routerLink="/lazy" aria-label="Go to lazy component">Go to Lazy Component</a> </nav> <router-outlet></router-outlet> `, imports: [RouterOutlet, RouterLink] }) export class AppComponent {} @Component({ standalone: true, selector: 'app-lazy', template: `<h2>This component was loaded lazily!</h2>`, }) export class LazyComponent {} export const routes = [ { path: 'lazy', loadComponent: () => import('./lazy.component').then(m => m.LazyComponent) } ]; import { bootstrapApplication } from '@angular/platform-browser'; bootstrapApplication(AppComponent, { providers: [provideRouter(routes)] });
Important Notes
Lazy loading works best with standalone components because they don't need modules.
Make sure your lazy loaded component is marked standalone: true.
Use aria-label on links for better accessibility.
Summary
Lazy loading delays loading components until needed, improving app speed.
Standalone components can be lazy loaded using loadComponent in routes.
This technique helps reduce initial load time and data usage.
Practice
1. What is the main benefit of lazy loading standalone components in Angular?
easy
Solution
Step 1: Understand lazy loading purpose
Lazy loading delays loading parts of the app until they are needed, reducing initial load time.Step 2: Connect lazy loading to standalone components
Standalone components can be lazy loaded to improve app speed by not loading them upfront.Final Answer:
It improves app speed by loading components only when needed -> Option DQuick Check:
Lazy loading = improves speed [OK]
Hint: Lazy loading means load only when needed [OK]
Common Mistakes:
- Thinking lazy loading bundles all components together
- Confusing lazy loading with automatic updates
- Believing lazy loading disables components on devices
2. Which syntax correctly lazy loads a standalone component in Angular routing?
easy
Solution
Step 1: Identify correct property for lazy loading standalone components
Angular usesloadComponentto lazy load standalone components in routes.Step 2: Check syntax correctness
{ path: 'home', loadComponent: () => import('./home.component').then(m => m.HomeComponent) } usesloadComponentwith dynamic import and then returns the component class, which is correct.Final Answer:
{ path: 'home', loadComponent: () => import('./home.component').then(m => m.HomeComponent) } -> Option AQuick Check:
Lazy load standalone = loadComponent [OK]
Hint: Use loadComponent with dynamic import for standalone lazy loading [OK]
Common Mistakes:
- Using component property instead of loadComponent
- Using loadChildren for components instead of modules
- Using non-existent loadModule property
3. Given this route config, what happens when navigating to '/dashboard'?
{ path: 'dashboard', loadComponent: () => import('./dashboard.component').then(m => m.DashboardComponent) }medium
Solution
Step 1: Understand loadComponent behavior
UsingloadComponentwith dynamic import delays loading the component until the route is accessed.Step 2: Apply to '/dashboard' route
The DashboardComponent will load only when the user navigates to '/dashboard', not before.Final Answer:
The DashboardComponent is loaded only when '/dashboard' is visited -> Option BQuick Check:
loadComponent = lazy load on route visit [OK]
Hint: loadComponent loads component on route visit only [OK]
Common Mistakes:
- Assuming component loads at app start
- Thinking component never loads
- Expecting runtime error without import
4. Identify the error in this route config for lazy loading a standalone component:
{ path: 'profile', loadComponent: import('./profile.component').then(m => m.ProfileComponent) }medium
Solution
Step 1: Check loadComponent syntax
TheloadComponentproperty must be a function returning a Promise, so it needs an arrow function wrapping the import.Step 2: Identify missing arrow function
The code calls import directly without wrapping in a function, causing the component to load eagerly or a runtime error when the router tries to invoke it.Final Answer:
Missing arrow function wrapping the import call -> Option AQuick Check:
loadComponent requires () => import(...) [OK]
Hint: Wrap import in arrow function for loadComponent [OK]
Common Mistakes:
- Calling import directly without function
- Confusing loadComponent with component property
- Thinking import must be at file top
5. You want to lazy load two standalone components,
AdminComponent and UserComponent, under routes '/admin' and '/user'. Which is the best way to configure the routes to optimize initial load time?hard
Solution
Step 1: Understand lazy loading standalone components
UsingloadComponentwith dynamic imports allows each component to load only when its route is visited, reducing initial load.Step 2: Compare options for multiple components
UseloadComponentwith dynamic imports for both routes separately loads each component lazily and separately, optimizing load time better than eager loading or bundling.Final Answer:
Use loadComponent with dynamic imports for both routes separately -> Option CQuick Check:
Separate loadComponent calls = best lazy loading [OK]
Hint: Lazy load each standalone component separately with loadComponent [OK]
Common Mistakes:
- Eagerly importing components defeats lazy loading
- Using loadChildren for standalone components unnecessarily
- Combining components increases initial load size
