← Back to Angular, page 2

angular-0024 · Medium mid

How does lazy loading work in the Angular router, and how do you configure loadComponent, loadChildren and preloading?

Lazy loading splits the app into chunks that are downloaded only when a route is first visited. Use loadComponent for a single standalone component and loadChildren for a group of child routes, both with a dynamic import(). A preloading strategy can fetch the remaining chunks in the background after start-up.

Full answer

By default, everything the app imports ends up in one initial bundle, so a big app loads slowly. Lazy loading moves rarely used areas, such as an admin panel, into separate chunks. The build tool (esbuild in the current CLI) creates a chunk wherever it sees a dynamic import().

Two forms in a route definition:

  • loadComponent: () => import('./report.component').then(m => m.ReportComponent) for one standalone component.
  • loadChildren: () => import('./admin.routes').then(m => m.ADMIN_ROUTES) for a whole child Routes array. If the file has a default export of the routes, import('./admin.routes') alone is enough.

The old NgModule form was loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule). It still works, and legacy projects use it.

The route's providers array can create services that live only in that lazy tree.

Preloading. After the first load, the router can fetch lazy chunks in the background so later navigation feels instant. withPreloading(PreloadAllModules) fetches all of them. NoPreloading is the default. For finer control, write a custom PreloadingStrategy that reads route.data['preload'], or preload on user hover.

How to check it works: run ng build and look at the output. Lazy chunks are listed separately from the initial bundle. A common mistake is importing a lazy component somewhere eagerly (for example in another component's imports), which pulls it back into the main bundle. In the browser's network tab you should see the chunk requested only when you navigate.

typescript
// file: app.routes.ts
import { Routes } from '@angular/router';

export const routes: Routes = [
  {
    path: 'report',
    loadComponent: () => import('./report.component').then((m) => m.ReportComponent),
  },
  {
    path: 'admin',
    loadChildren: () => import('./admin.routes').then((m) => m.ADMIN_ROUTES),
  },
];

// file: admin.routes.ts
import { Routes } from '@angular/router';
import { AdminHomeComponent } from './admin-home.component';

export const ADMIN_ROUTES: Routes = [
  { path: '', component: AdminHomeComponent },
];

// file: admin-home.component.ts
import { Component } from '@angular/core';

@Component({ selector: 'app-admin-home', template: `<h1>Admin home</h1>` })
export class AdminHomeComponent {}

// file: report.component.ts
import { Component } from '@angular/core';

@Component({ selector: 'app-report', template: `<h1>Report</h1>` })
export class ReportComponent {}

// file: app.config.ts
import { ApplicationConfig } from '@angular/core';
import { PreloadAllModules, provideRouter, withPreloading } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes, withPreloading(PreloadAllModules))],
};

Angular 20.3. Place the files side by side in `src/app`. After `ng build` the admin and report code appear in their own chunk files.

Key points

  • `import()` creates a chunk; `loadComponent` and `loadChildren` use it for routes.
  • Child route arrays are exported from their own file and loaded with `loadChildren`.
  • `withPreloading(PreloadAllModules)` fetches chunks after start-up. A custom strategy gives control.
  • An eager import of the same file defeats lazy loading.

Common mistakes

  • Importing the lazy component in the app's root `imports`, which silently puts it back in the main bundle.
  • Lazy loading tiny components, where the extra network request costs more than it saves.
  • Not checking the build output to confirm the chunk exists.

Follow-ups

  • How would you preload only some routes?
  • What does `canMatch` have to do with lazy loading?
  • How do route-level `providers` change the lifetime of a service?

Tips

  • Say how you verify it: `ng build` output and the network tab.
  • Mention the eager import trap. It shows real debugging experience.
  • Name the legacy NgModule form so the interviewer knows you can maintain old code.

Evaluation rubric

  • Writes loadComponent and loadChildren correctly.
  • Knows preloading options.
  • Knows how to verify and the common trap.