0

In my angular 11 application, I am using the lazyloading routing feature and share module feature. If I import the same module in two different feature modules, then I found angular download another middle js file. Below is my sample code.

CustomersModule:

import { CustomersRoutingModule } from './customers-routing.module';
import { CustomersComponent } from './customers.component';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [CustomersComponent],
  imports: [CommonModule, CustomersRoutingModule, FormsModule],
})
export class CustomersModule {}

OrdersModule:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { OrdersRoutingModule } from './orders-routing.module';
import { OrdersComponent } from './orders.component';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [OrdersComponent],
  imports: [CommonModule, OrdersRoutingModule, FormsModule],
})
export class OrdersModule {}

AppRoutingModule:

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

const routes: Routes = [
  { path: '', redirectTo: '/customers', pathMatch: 'full' },
  {
    path: 'customers',
    loadChildren: () =>
      import('./customers/customers.module').then((m) => m.CustomersModule),
  },
  {
    path: 'orders',
    loadChildren: () =>
      import('./orders/orders.module').then((m) => m.OrdersModule),
  },
];

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

Below is the browser network tab screenshot.

enter image description here

It's showing that an extra js file is loaded with name default~customers-customers-module~orders-orders-.module.js . Github link https://github.com/mustafizur8888/angular-lazyloading So how to solve it? Thanks.

  • I doubt if that is caused by the code you’ve shown. Do you have a folder named ‘default’ in your project? – MikeOne Jan 14 '21 at 17:47
  • @MikeOne no I don't have any folder name called default. Here is Github link. https://github.com/mustafizur8888/angular-lazyloading – Md. Mustafizur Rahman Jan 15 '21 at 05:45
  • I stumbled on that a year ago. A very kind man suggested me this https://medium.com/angular-in-depth/angular-code-splitting-or-how-to-share-components-between-lazy-modules-432c755e389c The bottom line is just that it is a normal behavior of lazy loading. – Buu97 Jan 18 '21 at 16:56

0 Answers0