Sub topic 10
Lazy Loading Components and Route Groups
This chapter assumes you know how to define a Routes array, register it with provideRouter, and nest child routes. It also assumes you are comfortable with standalone components and dynamic import() in TypeScript.
By default, every component referenced in your route table ends up in the main JavaScript bundle. Lazy loading moves a route's code into a separate chunk. The browser downloads that chunk the first time a user navigates to the route, which keeps the initial download small.
The Two Lazy Loading Hooks
Angular gives you two route properties for lazy loading:
| Property | Loads | Use it for |
|---|---|---|
| `loadComponent` | One standalone component | A single page |
| `loadChildren` | A `Routes` array | A whole feature area with its own child routes |
Both take a function that returns a dynamic import(). The build tool sees the import() call and splits the target file into its own chunk.
// app.routes.ts
import { Routes } from '@angular/router';
import { HomeComponent } from './home.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{
path: 'settings',
loadComponent: () =>
import('./settings/settings.component').then(m => m.SettingsComponent)
},
{
path: 'admin',
loadChildren: () =>
import('./admin/admin.routes').then(m => m.ADMIN_ROUTES)
}
];The admin feature defines its own route group. Its paths are relative to the parent admin segment.
// admin/admin.routes.ts
import { Routes } from '@angular/router';
import { AdminDashboardComponent } from './admin-dashboard.component';
import { AdminUsersComponent } from './admin-users.component';
export const ADMIN_ROUTES: Routes = [
{ path: '', component: AdminDashboardComponent }, // /admin
{ path: 'users', component: AdminUsersComponent } // /admin/users
];The components inside admin.routes.ts are imported eagerly within that file. That is fine, because the file as a whole is only loaded when the user visits /admin.
Default Exports
If a component or route file uses export default, you can skip the .then() call:
// settings/settings.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-settings',
standalone: true,
template: `<h2>Settings</h2>`
})
export default class SettingsComponent {}
// in app.routes.ts:
// { path: 'settings', loadComponent: () => import('./settings/settings.component') }What Happens at Navigation Time
The router caches the loaded module, so the network request only happens once per session.
Common Errors
- Eager import elsewhere. If any eagerly loaded file has a static
import { SettingsComponent } from ...line, the component goes back into the main bundle and you lose the split. Reference lazy components only throughimport(). - Wrong export name.
m => m.AdminRouteswhen the file exportsADMIN_ROUTESgives youundefined, and the navigation fails at runtime. - Repeating the parent path. Writing
path: 'admin/users'insideADMIN_ROUTESproduces/admin/admin/users. - Using
componentwithloadComponent. A route takes one or the other. Setting both is a configuration error.
Exercise
You add { path: 'reports', loadComponent: () => import('./reports.component').then(m => m.ReportsComponent) }, but the build output shows no separate chunk for ReportsComponent. What is the most likely cause?
Show answer
Some eagerly loaded file, such as app.component.ts or app.routes.ts, statically imports ReportsComponent. That pulls it into the main bundle. Remove the static import so the dynamic import() is the only reference.
Exercise
Inside a route group loaded with loadChildren at path 'shop', which path value makes a component render at /shop/cart?
Show answer
'cart'. Child paths are relative to the parent segment 'shop'.