其他分享
首页 > 其他分享> > Angular共享特性模块

Angular共享特性模块

作者:互联网

创建共享模块能让你更好地组织和梳理代码。你可以把常用的指令、管道和组件放进一个模块中,然后在应用中其它需要这些的地方导入该模块。

想象某个应用有下列模块:

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

请注意以下几点:

通过重新导出 CommonModule 和 FormsModule,任何导入了这个 SharedModule 的其它模块,就都可以访问来自 CommonModule 的 NgIf 和 NgFor 等指令了,也可以绑定到来自 FormsModule 中的 [(ngModel)] 的属性了。

即使 SharedModule 中声明的组件没有绑定过 [(ngModel)],而且 SharedModule 也不需要导入 FormsModule,SharedModule 仍然可以导出 FormsModule,而不必把它列在 imports 中。 这种方式下,你可以让其它模块也能访问 FormsModule,而不用直接在自己的 @NgModule 装饰器中导入它。

标签:FormsModule,SharedModule,导入,模块,CommonModule,import,共享,Angular
来源: https://www.cnblogs.com/varden/p/13867248.html