Component-Level Lazy Loading in Angular: Direct Routing with loadComponent
Component-Level Lazy Loading: Route directly to standalone components with loadComponent without intermediary files.
In legacy Angular architectures, lazy loading always required an intermediate NgModule. If you wanted to lazy-load a single screen or page, you were forced to create a dedicated feature module (e.g., dashboard.module.ts), configure routes inside RouterModule.forChild(), and load the bundle via loadChildren.
With standalone architecture, Angular cuts through this administrative overhead. The loadComponent route property allows you to lazy-load individual standalone components directly in your route configuration—bypassing intermediate module files, barrel files, and child routing modules entirely.
## 1. Architectural Shift: loadChildren vs. loadComponent
LEGACY MODULE-BASED LAZY LOADING:
```text
[app-routing.module.ts] ──> [feature.module.ts] ──> [feature-routing.module.ts] ──> [Component]
(3 to 4 ceremony files just to lazy-load one view)
```
MODERN COMPONENT-LEVEL LAZY LOADING:
```text
[app.routes.ts] ───────────────────────(loadComponent)────────────────────────> [Component]
(Direct, single-file resolution)
```
| Dimension | Legacy Lazy Loading (loadChildren) | Modern Component Lazy Loading (loadComponent) |
| --- | --- | --- |
| Target | An @NgModule class | A standalone @Component class |
| File Footprint | 3–4 files per feature (.module.ts, -routing.module.ts, .component.ts) | 1 file (.component.ts) |
| Routing Wiring | RouterModule.forChild(routes) inside an NgModule | Point routes directly to the component file |
| Tree-Shaking | Coarse; module boundaries frequently bundle unused shared utilities | Fine-grained; only the component and its declared imports are bundled |
| Code Splitting | Generates a chunk for the entire feature module | Generates an isolated chunk strictly for that component |
## 2. Basic Syntax and Direct Implementation
### 2.1 The Standalone Target Component
The target component must be a standalone component. It explicitly imports its own template dependencies (pipes, directives, child components) inside its @Component decorator:
```ts
// src/app/features/analytics/analytics-dashboard.component.ts
import { Component, signal } from '@angular/core';
import { CurrencyPipe, DatePipe } from '@angular/common';
@Component({
selector: 'app-analytics-dashboard',
standalone: true,
imports: [CurrencyPipe, DatePipe],
template: `
`
})
export class AnalyticsDashboardComponent {
today = new Date();
revenue = signal(148500);
}
```
### 2.2 Direct Route Wiring with loadComponent
In your routing configuration (app.routes.ts), reference the component file directly using dynamic ES module imports (import(...)):
```ts
// src/app/app.routes.ts
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'analytics'
},
{
path: 'analytics',
// Lazily downloads AnalyticsDashboardComponent as an isolated JS chunk
loadComponent: () =>
import('./features/analytics/analytics-dashboard.component')
.then(m => m.AnalyticsDashboardComponent)
}
];
```
When a user navigates to /analytics, the browser requests only the isolated chunk for AnalyticsDashboardComponent. If the user never visits that path, the code is never downloaded.
## 3. Cleaner Syntax: Default Exports (No .then() Needed)
Writing .then(m => m.AnalyticsDashboardComponent) on every route can get tedious. Angular automatically resolves default exports out of the box.
If you export your standalone component as default, you can omit the .then() chain entirely:
### Step 1: Export as Default
```ts
// src/app/features/settings/settings.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-settings',
standalone: true,
template: `
loadComponent: () =>
import('./admin/overview.component').then(m => m.OverviewComponent)
},
{
path: 'users',
// 3. Lazy-load the users tab inside
loadComponent: () =>
import('./admin/manage-users.component').then(m => m.ManageUsersComponent)
}
]
}
];
```
```html
```
## 6. Preloading Lazy Standalone Components
Lazy loading keeps your initial bundle lean, but users on slower connections might experience a slight delay when clicking a route for the first time.
You can configure Angular's preloading strategies to download lazy chunks in the background right after the initial application renders:
```ts
// src/app/app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withPreloading, PreloadAllModules } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(
routes,
// Preloads all loadComponent and loadChildren chunks in the background
withPreloading(PreloadAllModules)
)
]
};
```
## 7. loadComponent vs. loadChildren: When to Use Which?
While loadComponent is ideal for individual screens, modern Angular still relies on loadChildren for multi-route feature groups:
```text
Need to lazy-load code?
│
┌────────────────┴────────────────┐
▼ ▼
Is it a single screen? Is it a multi-route
│ feature (5+ screens)?
▼ │
Use loadComponent ▼
(() => import('./page.ts')) Use loadChildren
(() => import('./routes.ts'))
```
- **Use loadComponent:** When the target route is a single standalone component.
Use loadChildren (without NgModules): When grouping an entire sub-tree of routes (e.g., /shop/...) in a separate shop.routes.ts file without loading them upfront:
```ts
// src/app/app.routes.ts
export const routes: Routes = [
{
path: 'shop',
// Lazy-loads the entire route configuration array from an external file
loadChildren: () => import('./shop/shop.routes').then(m => m.SHOP_ROUTES)
}
];
```
Executive Analytics
Report Date: {{ today | date:'mediumDate' }}
Quarterly Revenue:
{{ revenue() | currency }}
