开发学院

您的位置:首页>教程>正文

教程正文

Angular 4 模块

Angular 4 模块

  Angular中的模块是指可以对与应用程序相关的组件、指令、管道和服务进行分组的地方。

  如果你正在开发一个网站,页眉、页脚、左、中、右部分将成为模块的一部分。

  要定义模块,我们可以使用NgModule。当您使用 Angular –cli 命令创建新项目时,默认情况下,ngmodule会在app.module.ts文件中创建,内容类似下面所示:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})

export class AppModule { }
The NgModule needs to be imported as follows ?

import { NgModule } from '@angular/core';
The structure for the ngmodule is as shown below ?

@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})

  它以@NgModule开头,包含一个具有声明、导入、提供者和引导的对象。

声明(Declaration)

  它是创建组件的数组,如果创建了新组件,它将首先被导入,引用将包含在声明中,如下所示:

declarations: [
   AppComponent,
   NewCmpComponent
]

导入(Import)

  它是应用程序中需要使用的一系列模块,声明数组中的组件也可以使用它。例如,现在在@NgModule中,我们看到浏览器模块被导入。如果你的应用程序需要表单,你可以包括如下模块

import { FormsModule } from '@angular/forms';
The import in the @NgModule will be like the following ?

imports: [
   BrowserModule,
   FormsModule
]

提供者(Providers)

  这将包括创建的服务。

引导(Bootstrap)

  这包括启动执行的主程序组件。