Monday, August 26, 2024

Understanding Lazy-Loaded Modules in Angular

Lazy-Loaded Modules in Angular

Lazy loading is a design pattern in Angular that allows you to load modules only when they are needed, rather than loading all modules upfront. This can significantly improve the performance of your application by reducing the initial load time.

What is a Lazy-Loaded Module?

A lazy-loaded module is an Angular module that is loaded on demand. Instead of being included in the main bundle, it is loaded asynchronously when the user navigates to a route that requires the module.

Example:

Let’s create a simple example to demonstrate lazy loading.

Step 1: Create a Feature Module

First, we’ll create a feature module that we want to lazy load.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeatureComponent } from './feature.component';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [FeatureComponent],
  imports: [
    CommonModule,
    RouterModule.forChild([
      { path: '', component: FeatureComponent }
    ])
  ]
})
export class FeatureModule {}
Step 2: Configure Lazy Loading in the App Routing Module

Next, we’ll configure the app routing module to lazy load the feature module.

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

const routes: Routes = [
  { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) }
];

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

In this example, the FeatureModule is lazy-loaded when the user navigates to the /feature route.

Impact on Dependency Injection Hierarchy

Lazy-loaded modules have their own injector, which is a child of the root injector. This has several implications for dependency injection:

  • Scoped Services: Services provided in a lazy-loaded module are scoped to that module. This means that each lazy-loaded module can have its own instance of a service, separate from the root injector and other lazy-loaded modules.
  • Service Isolation: Services provided in a lazy-loaded module are not available to the rest of the application unless explicitly provided at a higher level (e.g., in the root module).
  • Memory Management: Lazy loading can help with memory management by loading services only when needed and potentially unloading them when the module is no longer in use.
Example:
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class RootService {
  constructor() {}
}

@Injectable()
export class LazyService {
  constructor() {}
}

In this example, RootService is provided at the root level and is available throughout the application. LazyService, on the other hand, is provided in a lazy-loaded module and is only available within that module.

Feature Module:
@NgModule({
  providers: [LazyService],
  // other module metadata
})
export class FeatureModule {}

Key Points

  • Lazy Loading: Modules are loaded on demand, improving initial load time and performance.
  • Scoped Services: Services in lazy-loaded modules are scoped to those modules, allowing for isolated instances.
  • Injector Hierarchy: Lazy-loaded modules have their own injectors, which are children of the root injector.

Benefits of Lazy Loading

  • Improved Performance: Reduces the initial load time by loading only the necessary modules.
  • Modular Architecture: Encourages a modular architecture by isolating features and their dependencies.
  • Better Resource Management: Loads resources only when needed, potentially reducing memory usage.

Lazy loading is a powerful feature in Angular that, when combined with hierarchical dependency injection, allows for efficient and modular application design.

Happy coding!! 😊

No comments:

Post a Comment

^ Scroll to Top