如何最大程度地减少Angular模块中的大量import语句?

时间:2019-06-10 11:24:13

标签: angular es6-class angular-module

在我的angular模块中,我有50行import语句来导入这些类并在@NgModule的声明部分中使用。

在C ++中,有一个include文件概念,您可以将所有类放在一个include文件中并使用该文件。如何通过隐藏导入组件的详细信息使模块更具可读性?

这是我的@NgModule模块文件的启动方式:

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以在功能模块旁边有另一个模块,该模块可以导入所有类并重新导出它们以供功能模块使用。

FeatureImportsModule.ts:

import { ComponentA } from 'path/to/file'
import { ComponentB } from 'path/to/file'
...


@NgModule({
  declarations: [
    ComponentA,
    ComponentB
    ....
    ],
  exports: [
    ComponentA,
    ComponentB,
    ...
  ]
})
export class FeatureImportsModule { } 

FeatureModule.ts,这是变得“更具可读性”的代码:

@NgModule({
  imports: [ FeatureImportsModule ]
export class FeatureModule { }

答案 1 :(得分:-2)

我认为,您应该将应用程序分成较小的模块(称为功能模块)。此后,您还可以进行延迟加载并在需要时加载一些导入。您可以在官方Angular教程https://angular.io/guide/feature-modules

中找到更多信息。
相关问题