Lazy Loading In Angular
In this article, we will learn how to use lazy loading. The term "lazy loading" refers to a process in which angular loads Modules just when needed rather than all at once. On-demand loading is another term for it. By default, Angular eagerly loads the modules. The initial load time of the app is reduced by using lazy loading of Angular Modules. When the user navigates to a route, we use the Angular Router's loadChilden function to lazy load them.
Why Lazy load?
As we add more functionality to our Angular apps, they grow in size. Angular Modules assist us in managing our app by allowing us to create different modules for each new feature. However, as the application grows in size, it takes longer to load. Because angular loads the complete application upfront, this is the case.
The user does not have a positive impression of the app because it takes so long to load. The app appears to run faster to the user when only a portion of the app is loaded (i.e. lazy loading). The faster loading software improves performance while also providing a positive user experience.
How Lazy loading works
Lazy loading in Angular is done at the module level. That is, only the Angular Modules can be lazy-loaded. Individual components cannot be loaded in a slow manner.
The Angular Router Module is used to implement lazy loading. The Angular Router's loadChildren function is in charge of loading the Modules.
When we define the routes, we specify the modules we want to load slowly.
We have a new syntax for lazy loading starting with Angular 8.
{path: "admin", loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)},
loadChildren
import('./admin/admin.module').then(m => m.AdminModule)
Angular Lazy Loading Example
Admin Module
{ path: 'admin', children :[ { path: 'dashboard', component: DashboardComponent}, { path: 'user', component: UserComponent}, { path: 'rights', component: RightsComponent}, ] },
import { Component } from '@angular/core'; @Component({ template: `<h1>Dashboard Component</h1>`, }) export class DashboardComponent { title = ''; }
Add the following code under the src/app/admin/pages/rights/rights.component.ts:
import { Component } from '@angular/core'; @Component({ template: '<h1>Rights Component</h1>', }) export class RightsComponent { title = ''; }
Add the following code under the src/app/admin/pages/user/user.component.ts:
import { Component } from '@angular/core'; @Component({ template: '<h1>User Component</h1>', }) export class UserComponent { title = ''; }
Add the following code under the src/app/admin/pages/index.ts:
export * from './dashboard/dashboard.component'; export * from './rights/rights.component'; export * from './user/user.component';
Add the following code under the src/app/admin/admin.routing.module.ts:
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { UserComponent , RightsComponent ,DashboardComponent } from './pages'; const routes: Routes = [ { path: 'admin', children :[ { path: 'dashboard', component: DashboardComponent}, { path: 'user', component: UserComponent}, { path: 'rights', component: RightsComponent}, ] }, ]; @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class AdminRoutingModule { }
Add the following code under the src/app/admin/admin.module.ts:
import { NgModule } from '@angular/core'; import { AdminRoutingModule } from './admin.routing.module'; import { UserComponent,RightsComponent,DashboardComponent } from './pages'; @NgModule({ declarations: [UserComponent,RightsComponent,DashboardComponent], imports: [ AdminRoutingModule, ], providers: [], }) export class AdminModule { }
export * from './admin.module'; export * from './pages';
Shared Module
import { Component } from '@angular/core'; @Component({ selector: 'app-footer', templateUrl: './footer.component.html' }) export class FooterComponent { }
Add the following code under the src/app/shared/layout/footer/footer.component.html:
<p>(c) All Rights Reserved</p>
Add the following code under the src/app/shared/layout/header/header.component.ts:
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-header', templateUrl: './header.component.html', styleUrls: ['./header.component.css'] }) export class HeaderComponent { }
Add the following code under the src/app/shared/layout/footer/footer.component.ts:
<ul> <li> <a class="navbar-brand" routerlink="">home</a> </li> <li> <a class="navbar-brand" routerlink="/admin/dashboard">Dashboard</a> </li> <li> <a class="navbar-brand" routerlink="/admin/rights">rights</a> </li> <li> <a class="navbar-brand" routerlink="/admin/user">user</a> </li> </ul>
Add the following code under the src/app/shared/layout/header/header.component.css:
ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 16px; text-decoration: none; } li a:hover { background-color: #111111; }
Add the following code under the src/app/shared/layout/index.ts:
export * from './footer/footer.component'; export * from './header/header.component';
Add the following code under the src/app/shared/shared.module.ts
import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { RouterModule } from '@angular/router'; import { HeaderComponent, FooterComponent } from './layout'; @NgModule({ imports: [ CommonModule, FormsModule, ReactiveFormsModule, HttpClientModule, RouterModule ], declarations: [ HeaderComponent,FooterComponent ], exports: [ CommonModule, FormsModule, ReactiveFormsModule, HttpClientModule, RouterModule, HeaderComponent,FooterComponent ] }) export class SharedModule { }
Add the following code under the src/app/shared/index.ts:
export * from './shared.module'; export * from './layout';
Root Module
import { Component } from '@angular/core'; import { HeaderComponent, FooterComponent } from './shared'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Module Demo'; }
<app-header></app-header> <h1>Lazy loaded module Demo</h1> <router-outlet></router-outlet> <app-footer></app-footer>
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = [ ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { SharedModule} from './shared'; import { AdminModule} from './admin'; @NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, AppRoutingModule, SharedModule, AdminModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Lazy loading the AdminModule
const routes: Routes = [ {path: "admin", loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)}, ];
const routes: Routes = [ { path: 'dashboard', component: DashboardComponent}, { path: 'user', component: UserComponent}, { path: 'rights', component: RightsComponent}, ];