如何使用延迟加载仅加载angular 8中的模块

问题描述 投票:2回答:2

我对延迟加载有疑问。使用Augury检测我的路由器树结构。

enter image description here

app-routing.module.ts

const routes: Routes = [
  {
    path: '',
    children: [
      {
       path: 'reservation',
          loadChildren: () => import('./modules/reservations/reservations.module').then(mod => mod.ReservationsModule)
      },
      {
        path: 'add',
        loadChildren: () => import('./modules/add-autopoint/add-autopoint.module').then(mod => mod.AddAutopointModule)
      },
      {
        path: 'availability',
        loadChildren: () => import('./modules/availability/availability.module').then(mod => mod.AvailabilityModule)
      },
      {
        path: 'archive',
        loadChildren: () => import('./modules/archive/archive.module').then(mod => mod.ArchiveModule)
      },
      { path: '**', redirectTo: '/reservation', pathMatch: 'full' },
    ]
  }
];

示例模块路由

reservation-routing.module.ts

const routes: Routes = [
  { path: '', component: ReservationComponent },
  { path: 'summary/:id', component: SummaryComponent, resolve: { summary: AtplSummaryResolver, notes: AtplNotesResolver } }
];

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    FilterPipe,
    LoaderComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    ReactiveFormsModule,
    SharedModule,
    NgxMatSelectSearchModule,
    FlexLayoutModule,
    AddAutopointModule,
    ArchiveModule,
    ReservationsModule,
    AvailabilityModule,
    Ng2LoadingSpinnerModule.forRoot({}),
    NavigationModule
  ],
  providers: [
    CommonService,
    AlertService,
    ListService,
    LoaderService,
    MenuService
  ],
  bootstrap: [AppComponent]
})

首先加载这些组件是否正常,或者我做错了什么?

angular lazy-loading angular8 angular-routing angular-router
2个回答
2
投票

仅在延迟加载的模块中使用的公共服务,组件,管道...应在共享模块中分组,并且仅在这些延迟加载的模块中导入此模块。整个应用程序中所需的其他组件,服务和其他事物(例如授权服务或指令,布局组件或服务...)将其放在App模块中导入的Core模块中。 Core模块(以及App模块中导入的其他模块)应尽可能轻巧。


0
投票

删除这些模块后:

AddAutopointModule

ArchiveModule

ReservationsModule

AvailabilityModule

从我的app.module.ts中,我解决了延迟加载的问题

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.