When you create a new Angular project using Angular CLI command, you get a fresh app along with a module called AppModule
. You can very well code the entire app in this module itself, but that won't be a good idea because it will make the loading of the app very slow depending on how big the app is. You must break a large app into feature modules so that you can:
In this guide, you will learn about the advantages of feature modules and lazy loading of a feature module, and understand the difference it makes for an app's load time.
Lazy Loading - One of the advantages of breaking the app into multiple feature modules is the ability to load code lazily on-demand. When a user hits an Angular app hosted on a web server, the browser downloads all necessary files for the app to run successfully. If the app is partitioned into multiple feature modules but if all of them are eagerly loaded, then all the feature modules are also downloaded, which increases the app's load time. But if some of the feature modules are lazily loaded, then the code for them is not downloaded on the app's first run. Code for lazily loaded feature modules is downloaded only when a user tries to access a functionality that is a part of a lazily loaded feature module.
Clean Code - Breaking the app into multiple feature modules makes the code clean, as all the artifacts of a particular module are grouped inside the folder of that module.
To complete this guide, you should have:
ng new feature-modules-demo
feature-modules-demo
. Select this folder and click Open in the file dialog box.You will install Twitter Bootstrap and jQuery and reference them in your project so that you can use some of the form styles to make the form look good.
cd feature-modules-demo
npm i jquery
npm i bootstrap
angular.json
.styles
array before src/style.css
."node_modules/bootstrap/dist/css/bootstrap.min.css",
scripts
array:"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
src > index.html
.class="container"
The final code should look like:
1<body class="container">
2 <app-root></app-root>
3</body>
Now the project is all set for development.
Run the following command to generate a user profile module:
ng generate module user-profile
ng generate component --module user-profile login
src > app > login > login.component.html
, delete the contents of the file and add following code:1<h1>Login</h1>
2<hr>
3<div class="col-md-4">
4 <form autocomplete="off">
5 <div class="form-group" >
6 <label for="userName">User Name:</label>
7 <input id="userName" type="text" class="form-control" placeholder="User Name..." />
8 </div>
9 <div class="form-group" >
10 <label for="password">Password:</label>
11 <input id="password" type="password" class="form-control"placeholder="Password..." />
12 </div>
13
14 <button type="submit" class="btn btn-primary">Login</button>
15 <button type="button" class="btn btn-default">Cancel</button>
16 </form>
17</div>
In Visual Studio Code, open the file src > app > user-profile > user-profile.module.ts
, delete the contents of the file, and add following code:
1import { NgModule } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { LoginComponent } from '../login/login.component';
4import { Routes, RouterModule } from '@angular/router';
5
6const routes:Routes = [
7 {path: 'login', component: LoginComponent}
8];
9
10@NgModule({
11 declarations: [LoginComponent],
12 imports: [
13 CommonModule,
14 RouterModule.forChild(routes)
15 ]
16})
17export class UserProfileModule { }
In Visual Studio Code, open the file src > app > app-routing.module.ts
, delete the contents of the file and add following code:
1import { NgModule } from '@angular/core';
2import { Routes, RouterModule } from '@angular/router';
3
4
5const routes: Routes = [
6 {path:'user', loadChildren: () => import(`./user-profile/user-profile.module`).then(m => m.UserProfileModule)}
7];
8
9@NgModule({
10 imports: [RouterModule.forRoot(routes)],
11 exports: [RouterModule]
12})
13export class AppRoutingModule { }
In Visual Studio Code, open the file src > app > app.component.html
, delete the contents of the file and add following code:
1<div class="row">
2 <div class="col-md-2">
3 <a [routerLink]="['user/login']">Login</a>
4 </div>
5</div>
6<div class="row">
7 <div class="col-md-12">
8 <router-outlet></router-outlet>
9 </div>
10</div>
In this section, you will notice the advantages of the lazily loaded user profile feature module.
ng serve
http://localhost:4200
.user-profile-user-profile-module.js
.user-profile-user-profile-module.js
is downloaded immediately.This is the advantage of lazily loaded feature modules where the code is not loaded in memory until needed.
In Visual Studio Code, open file src > app > app-routing.module.ts
, delete the contents of the file, and add following code:
1import { NgModule } from '@angular/core';
2import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
3
4
5const routes: Routes = [
6 {path:'user', loadChildren: () => import(`./user-profile/user-profile.module`).then(m => m.UserProfileModule)}
7];
8
9@NgModule({
10 imports: [RouterModule.forRoot(routes, {
11 // Tell the router to use the HashLocationStrategy.
12 useHash: true,
13 enableTracing: false,
14
15 // This will tell Angular to preload the lazy-loaded routes after the
16 // application has been bootstrapped. This will extend to both top-level
17 // and nested lazy-loaded modules.
18 preloadingStrategy: PreloadAllModules
19 })],
20 exports: [RouterModule]
21})
22export class AppRoutingModule { }
http://localhost:4200
user-profile-user-profile-module.js
gets loaded automatically without clicking the Login link.This is the second advantage of lazily loaded feature modules. The feature modules were loaded in the background, after the first rendering of the app was complete.
In this guide, you have learned:
For more information, follow the Lazy loading ngmodules guide.