angular应用的模块系统被称为angular模块或NgModule
- 定义:
///angular是带有@NgModule装饰器提供元数据的类。 其模块系统被称为angular模块或NgModules.每一个angular应用至少有一个模块,不可或缺的根模块(如:AppModule)。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
- 作用:
1.angular模块把组件、指令、管道打包成内聚功能块;
2.angular模块用于添加服务(外部或自定义,如http请求类、路由类等);
3.angular模块在应用启动时,可以主动或懒加载;
- @NgModule:
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
});
//imports--angular模块把特性合并成离散单元的一种方式,当应用需要模块的特性时,将其添加到imports数组中,它告诉Angular应用需要它们来正常工作。
//providers--创建的服务加入到当前模块中,如果是根模块,则可以用于应用任何部分。
//declarations--声明当前module控制的组件,创建的指令和管道也要添加至declarations数组中。
//exports--declarations 的子集,可用于其它模块的组件模板。
//bootstrap--指定应用的主视图(称为根组件),它是所有其它视图的宿主。只有根模块才能设置bootstrap属性。