Lazy Loading in Angular

Abhay NirmalAbhay Nirmal
2 min read

Why should we use this feature?

  • Lazy Loading helps keep initial bundle sizes smaller, which in turn helps decrease load time and see our site blinks faster on user screen.

  • Lazy Loading conserves bandwidth by delivering content to users only if itโ€™s requested (why do we load data that is unnecessary to that particular route๐Ÿคจ).

What is Lazy Loading?

  • Lazy Loading is the practice to load data only when it is necessary.

    For Example, Post list and User's detail screen.

  • We don't want to load posts and profile data on home page.

    • We have two routes.

      1. /posts

      2. /profile

    • On the "/posts" route we require to load only posts module.

    • On the "/profile" route we require to load profile module.

How can we implement it?

  • open directory src/app/app-routing.module.ts in your project.
const routes: Routes = [
{
    path: '',
    component:'HomeComponent'
},  
{
    path: 'posts',
    loadChildren: () => import('./posts/posts.module').then(m => m.PostsModule)
},
{
    path: 'profile',
    loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule)
}
];

Now on your Home page, there are two buttons eg. "Posts" and "Profile".

Before we move on to verify this feature let's build the component-routing module first.

  1. post-routing.module.ts

    ```typescript import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router';

    import { PostComponent } from './post.component';

const routes: Routes = [
  {
    path: '',
    component: PostComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class PostRoutingModule { }
```
  1. profile-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { ProfileComponent } from './profile.component';


const routes: Routes = [
  {
    path: '',
    component: ProfileComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ProfileRoutingModule { }

However, we can verify this feature from network tab of chrome development tools.

Most common mistakes!!

  1. Importing common modules in multiple places within an application.

  2. The same component cannot be imported in both app.module.ts and in "your-new-module.module.ts"

  3. "your-component" is not part of any NgModule or the module has not been imported into your module.

    • This error could happen when:

      1. Component has not been declared anywhere.

      2. Component has been declared once in a feature module but component is still declared in a higher level route (e.g. app.routes) or in another feature module.

    • "A component must be declared, and only once"

  4. "TypeError: undefined is not a function"

    • Check that lazy loaded modules are NOT imported anywhere.

    • Rebuild app (sometimes I had to do it more than once..๐Ÿ˜…๐Ÿค”).

For more information angular.io-lazy-loading-modules
Happy Codding ๐Ÿ™‚๐Ÿ™‚

1
Subscribe to my newsletter

Read articles from Abhay Nirmal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhay Nirmal
Abhay Nirmal

I'm skilled at c++, c#, python, javascript,angular and flutter and I'm always enthusiastic for grabbing onto other technologies and programming languages which I can learn and apply. I'm keen to learn new tech stacks on chatGPT and using it to make things easier.