Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Lazy Loading

AngularLesson 3625 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Lazy Loading
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Lazy Loading, in plain language

Lazy Loading only loads modules when they're actually needed, shrinking the initial bundle size.

Problem → Why → Solution

The problem — why this concept matters

Lazy-loading every tiny component can add request overhead, UX flicker, and worse bundle design.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Lazy loading splits route or component code out of the initial bundle and fetches it when needed. Choose boundaries around user features and plan loading and error UX, preloading, and shared dependency duplication. Lazy-route provider scopes may create new service instances, so state lifetime must be deliberate.

Mental model
  1. URL or navigation
  2. router config and guard
  3. component view

Explain the core concept and Angular data flow of Lazy Loading

Core syntax or pattern

typescript
// app-routing.module.ts
const routes: Routes = [
  {
    path: 'admin',
    loadChildren: () => import('./admin/admin.module')
      .then(m => m.AdminModule)
  },
  {
    path: 'users',
    loadChildren: () => import('./users/users.module')
      .then(m => m.UsersModule)
  }
];

// admin-routing.module.ts (inside admin folder)
const routes: Routes = [
  { path: '', component: AdminDashboardComponent },
  { path: 'users', component: AdminUsersComponent },
  { path: 'settings', component: AdminSettingsComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }

Lazy loading splits route or component code out of the initial bundle and fetches it when needed. Choose boundaries around user features and plan loading and error UX, preloading, and shared dependency duplication. Lazy-route provider scopes may create new service instances, so state lifetime must be deliberate.

Read the code step by step

  1. URL or navigation// app-routing.module.ts

    Lazy Loading only loads modules when they're actually needed, shrinking the initial bundle size.

  2. router config and guard];

    Lazy loading splits route or component code out of the initial bundle and fetches it when needed. Choose boundaries around user features and plan loading and error UX, preloading, and shared dependency duplication. Lazy-route provider scopes may create new service instances, so state lifetime must be deliberate.

  3. component viewexport class AdminRoutingModule { }

    Lazy-load an admin feature and verify a separate chunk, direct loading, and loading or failure behavior through build or network evidence.

Execution flow
  1. URL or navigation
  2. router config and guard
  3. component view

Success

Explain the core concept and Angular data flow of Lazy Loading

Failure

Lazy-loading every tiny component can add request overhead, UX flicker, and worse bundle design.

Lazy Loading only loads modules when they're actually needed, shrinking the initial bundle size.

typescript
// app-routing.module.ts
const routes: Routes = [
  {
    path: 'admin',
    loadChildren: () => import('./admin/admin.module')
      .then(m => m.AdminModule)
  },
  {
    path: 'users',
    loadChildren: () => import('./users/users.module')
      .then(m => m.UsersModule)
  }
];

// admin-routing.module.ts (inside admin folder)
const routes: Routes = [
  { path: '', component: AdminDashboardComponent },
  { path: 'users', component: AdminUsersComponent },
  { path: 'settings', component: AdminSettingsComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }
You should see
(The Admin module loads only when you navigate to the /admin route, which improves performance)

Real-world usage

How to apply it in a real project

Lazy-load an admin feature and verify a separate chunk, direct loading, and loading or failure behavior through build or network evidence.

Mini challenge

Build it yourself

Rebuild the Lazy Loading example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// app-routing.module.ts
const routes: Routes = [
  {
    path: 'admin',
    loadChildren: () => import('./admin/admin.module')
      .then(m => m.AdminModule)
  },
  {
    path: 'users',
    loadChildren: () => import('./users/users.module')
      .then(m => m.UsersModule)
  }
];

// admin-routing.module.ts (inside admin folder)
const routes: Routes = [
  { path: '', component: AdminDashboardComponent },
  { path: 'users', component: AdminUsersComponent },
  { path: 'settings', component: AdminSettingsComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }

Expected: Explain the core concept and Angular data flow of Lazy Loading

Show hint

Lazy-load an admin feature and verify a separate chunk, direct loading, and loading or failure behavior through build or network evidence.

Quick quiz 1 / 3

What best describes the core idea of Lazy Loading?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • URL or navigation → router config and guard → component view
  • Lazy loading splits route or component code out of the initial bundle and fetches it when needed. Choose boundaries around user features and plan loading and error UX, preloading, and shared dependency duplication. Lazy-route provider scopes may create new service instances, so state lifetime must be deliberate.
  • Lazy-loading every tiny component can add request overhead, UX flicker, and worse bundle design.
  • Lazy-load an admin feature and verify a separate chunk, direct loading, and loading or failure behavior through build or network evidence.

Angular — Routing — Angular

Easy traps

  • Lazy-loading every tiny component can add request overhead, UX flicker, and worse bundle design.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Lazy Loading example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (The Admin module loads only when you navigate to the /admin route, which improves performance)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Lazy Loading | Thuta Learning