角模块摇树

时间:2020-03-18 13:40:10

标签: angular

假设我有一个模块可以导入我在应用程序的其他位置定义的NgModule({ declarations: [ ComponentOne, ComponentTwo, ComponentThree ] }) export class CommonModule {}

imports

当我在其他地方使用此模块时,通过<component-one></component-one>仅将用于模板的组件包含在最终捆绑包中吗?换句话说:

int result = 1 + random.nextInt(100);

最终的捆绑包是否仅包括此组件?摇树在这里生效吗?

3 个答案:

答案 0 :(得分:0)

您还必须将需要重用的所有组件都包含在exports数组中。

答案 1 :(得分:0)

有效期直到Angular 8

不属于entryComponents的所有组件都将通过摇树删除。

entry components

事实上,许多库声明和导出了您永远不会使用的组件。例如,材料设计库将导出所有组件,因为它不知道您将使用哪个组件。但是,您不太可能会全部使用它们。对于您不参考的组件,摇树器会将这些组件从最终代码包中删除。

如果组件不是入口组件,并且在模板中找不到,则摇树器会将其丢弃。因此,最好仅添加真正的入门组件,以帮助您的应用保持尽可能的精简。

答案 2 :(得分:0)

@aelagawy的答案是正确的,为了使用组件,您应该将其导出

请在此LINK

的官方文档中查看详细信息

创建共享模块可以组织和简化您的 码。您可以将常用的指令,管道和组件放入 一个模块,然后将其导入到您需要的任何位置 您应用的其他部分。

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CustomerComponent } from './customer.component';
import { NewItemDirective } from './new-item.directive';
import { OrdersPipe } from './orders.pipe';

@NgModule({
 imports:      [ CommonModule ],
 declarations: [ CustomerComponent, NewItemDirective, OrdersPipe ],
 exports:      [ CustomerComponent, NewItemDirective, OrdersPipe,
                 CommonModule, FormsModule ]
})
export class SharedModule { }

再次获得更多详细信息,请检查此链接 https://angular.io/guide/sharing-ngmodules