How to use loadChildren - Lazyloading in Angular

Angular - How to use loadChildren - Lazyloading in Angular, ANGULAR WITH CORE .NET AND MVC RAZOR ENGINE

  Lazy loading in Angular

  • Lazy loading modules in Angular allows you to load modules when it is actually necessary.
  • Lazy loading will help you to achieve performance.
  • Lazy loading will help you to decrease the size.
  • By using loadChildren(), we can implement Lazy loading in Angular 
Let's take an example to understand it
  •      Create a project, which will load different components from a different module.

We will follow below method to implement lazy loading
  • Step 1 : Create a new project using command ng new project-name
  • Step 2 : Create  3 different module to implement lazy loading 
  • Step 3 : Open app\app-routing.module.ts and place below code 
    import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'college', <b>loadChildren</b>: () =&amp;; import('./college/college.module').then(college =&gt;; // tslint:disable-next-line: no-unused-expression college.CollegeModule ) }, { path: 'student', loadChildren: () =&gt;; import('./student/student.module').then(student =&gt;; // tslint:disable-next-line: no-unused-expression student.StudentModule ) }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }

      loadChildren -> This is lazy loading 😊

  • Step 4: Open  app.module.ts and place and replace import with below code
    imports: [ BrowserModule, AppRoutingModule, CollegeModule, StudentModule ],
  • Step 5: open app.component.html and place below code 
  • <h1> <a home="" href="https://www.blogger.com/u/1/null" routerlink="">Home Page From App Module </a> </h1> <h1> <a college="" href="https://www.blogger.com/u/1/null" routerlink="">Colleg Page from College Module </a> </h1> <h1> <a href="https://www.blogger.com/u/1/null" routerlink="" student="">Student Page from College Student </a> </h1> <hr /> <router-outlet></router-outlet>
  • Step 6:  hit ng serve command. it will start working

Comments

Post a Comment

Popular posts from this blog

How to disable table row in Angular

How to create XML file dynamically in c#.net?

How to get Array value in Angular ?