Angular7:模块
Angular模块指的是一个可以对与应用程序相关的组件、指令、管道和服务进行分组的地方。
如果你正在开发一个网站,页眉、页脚、左侧、中间和右侧部分可以成为模块的一部分。
要定义模块,我们可以使用NgModule。使用 Angular –cli 创建新项目时,默认情况下,在app.module.ts文件中创建该模块,如下所示
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { NewCmpComponent } from './new-cmp/new-cmp.component'; @NgModule({ declarations: [ AppComponent, NewCmpComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
NgModule需要按如下方式导入:
import { NgModule } from '@angular/core';
ngmodule的结构如下所示:
@NgModule({ declarations: [ AppComponent, NewCmpComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] })
它以@NgModule开头,包含一个对象,该对象有声明, 导入, 提供者和引导程序。
声明
它是创建的组件数组。如果创建了任何新组件,它将首先被导入,引用将包含在声明中,如下所示
declarations: [ AppComponent, NewCmpComponent ]
导入
它是应用程序中需要使用的一系列模块。声明数组中的组件也可以使用它。例如,现在在@NgModule中,我们看到浏览器模块被导入。如果您的应用程序需要表单,您可以包含具有以下代码的模块
import { FormsModule } from '@angular/forms';
@NgModule中的导入将如下所示:
imports: [ BrowserModule, FormsModule ]
提供者
包括创建的服务。
引导程序
包括开始执行的主要应用程序组件。