Sub topic 4
Lazy loading a single component with loadComponent
This chapter assumes you can define routes with the Angular Router, write standalone components, and run ng build. It also assumes you know that import() returns a Promise.
What loadComponent does
A route normally names its component with component. That forces the component, and everything it imports, into the bundle that contains the route table. The loadComponent property replaces component. It takes a function that returns a Promise of a component class (or an Observable of one). The router calls that function the first time the route is activated.
Because the function uses a dynamic import(), the bundler emits the component and its private dependencies as a separate chunk. The router downloads that chunk on first navigation, then reuses it.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
loadComponent: () =>
import('./home/home.component').then(m => m.HomeComponent),
},
];Read the arrow function in three parts:
import('./home/home.component')asks the bundler for a chunk. It resolves to the module's exports..then(m => m.HomeComponent)picks the component class out of those exports.- The router receives the class and renders it in the
<router-outlet>.
Eager and lazy side by side
The difference shows up in the imports at the top of the route file.
import { Routes } from '@angular/router';
import { ReportsComponent } from './reports/reports.component';
// Eager: ReportsComponent is in the main bundle.
export const eagerRoutes: Routes = [
{ path: 'reports', component: ReportsComponent },
];import { Routes } from '@angular/router';
// Lazy: no static import of ReportsComponent anywhere.
export const lazyRoutes: Routes = [
{
path: 'reports',
loadComponent: () =>
import('./reports/reports.component').then(m => m.ReportsComponent),
},
];The lazy version has no top-level import of the component. A static import anywhere in the main bundle's dependency graph pulls the component back into that bundle, and the split is lost.
Default exports shorten the call
If the component file uses a default export, import() resolves to an object whose default property is the class. The router accepts a default export directly, so you can skip the .then.
// reports/reports.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-reports',
standalone: true,
template: `<h1>Reports</h1>`,
})
export default class ReportsComponent {}// app.routes.ts
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'reports',
loadComponent: () => import('./reports/reports.component'),
},
];Named exports are still the more common style in many codebases. Pick one convention and keep it consistent.
What the user sees while loading
The router does not render anything for the new route until the chunk arrives. The previous view stays on screen. On a slow connection this can feel like a stalled click. You can observe the delay with router events: NavigationStart fires when navigation begins, and NavigationEnd fires after the chunk has loaded and the component is activated. A common approach is to show a progress bar between those two events.
Common mistakes
- Static import left behind. An unused import is often removed by tree shaking, but a used one, such as a type, a constant, or another component, keeps the file in the main bundle. Check that nothing outside the lazy chunk imports the component file.
- Wrong export name.
m.ReportsComponentisundefinedif the class is named differently. The router then fails at navigation time, not at build time in some setups, so test every lazy route. - Lazy loading the landing page. If the route at
''is almost always the first thing users see, a lazy chunk adds a second request before anything renders. Loading it eagerly is often faster. Measure before deciding. - Tiny components. A chunk has request overhead. Splitting a 2 KB component rarely pays off. Split at feature level, which the next chapter covers with
loadChildren.
Try it
Convert one route in your own app, run ng build, and look for a new chunk-*.js file in the lazy chunks section of the output. A later chapter in this tutorial covers reading that output in detail.
Exercise
A route is declared as { path: 'help', loadComponent: () => import('./help.component').then(m => m.HelpComponent) }, but ng build produces no separate chunk for it. app.component.ts contains import { HelpComponent } from './help.component'; and lists it in its imports array. Why does the build produce no chunk, and what is the fix?
Show answer
The static import in app.component.ts puts help.component.ts in the main bundle's dependency graph, so the bundler cannot split it out. The import() call then resolves to code already in the main bundle. The fix is to remove the static import and the imports entry from app.component.ts, and to render the component only through the route.
Exercise
What does the router do with a lazy route's component while the chunk is downloading, and where should a loading indicator live?
Show answer
The router renders nothing for the new route until the chunk arrives, and the previous view stays visible. The indicator should live in a component that already exists, such as the root component, and be driven by NavigationStart and NavigationEnd router events.