Angular 共享模块导致加载其他共享模块

时间:2021-01-14 17:29:13

标签: angular lazy-loading angular11 shared-module

在我的 angular 11 应用程序中,我使用了延迟加载路由功能和共享模块功能。 如果我在两个不同的功能模块中导入同一个模块,那么我发现angular下载了另一个中间js文件。下面是我的示例代码。

客户模块:

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 {}

订单模块:

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 {}

应用路由模块:

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 {}

以下是浏览器网络标签截图。

enter image description here

它显示加载了一个名为 default~customers-customers-module~orders-orders-.module.js 的额外 js 文件。 Github 链接 https://github.com/mustafizur8888/angular-lazyloading 那么如何解决呢?谢谢。

0 个答案:

没有答案