How to Lazy Load Routes ?
Step 1: Create a new Angular application using the following command.
ng new sample-app
cd sample-app
Step 2: Create the 4 components using the following commands.
ng g c home
ng g c services
ng g c about-us
ng g c contact-us
Folder Structure:
Dependencies:
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Now we can update our app.router.ts file to add the actual routes. Also don’t forget to add router-outlet tag in the app.component.html and import RouterOutlet to app.component.ts. Finally, we can add the buttons to handle navigation between different routes, for this to work we will need to import the RouterLink.
With Angular 16+, it has become much simpler to lazy load any route, we can make use of the loadComponent property to load a component or loadChildren to load nested routes.
In the below example, we have four components, out of which home and services are loaded initially with the app. While Contact Us and About Us component are loaded lazily.
<!-- app.component.html -->
<button routerLink="home">Home</button>
<button routerLink="services">Services</button>
<button routerLink="about-us">About Us</button>
<button routerLink="contact-us">Contact Us</button>
<router-outlet></router-outlet>
//app.router.ts
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full',
},
{
path: 'home',
component: HomeComponent
},
{
path: 'services',
component: ServicesComponent,
},
{
path: 'about-us',
loadComponent: () => import('./about-us/about-us.component').then((c) => c.AboutUsComponent)
},
{
path: 'contact-us',
loadComponent: () => import('./contact-us/contact-us.component').then((c) => c.ContactUsComponent)
}
]
// app.component.ts
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'sample-app';
}
Output:
We can see the output of our build process, contact-us-component and about-us-component are loaded into different output file each so that they can be served only when required.
Implementing Lazy Loading in Angular
Angular applications often consist of various modules and components, which can result in longer initial load times, especially for larger applications. To address this issue and improve performance, Angular provides lazy loading—a technique that defers the loading of certain modules until they are needed.
In this article, we’ll learn more about lazy loading in Angular and how we can implement it in our project.
Table of Content
- What is Lazy Loading?
- Why Use Lazy Loading ?
- How to Lazy Load Routes ?
- How to Lazy Load Nested Routes ?