模块是Angular应用程序的重要组成部分。一个Angular应用程序由多个模块组成,必定有一个根模块,一个模块由多个组件组成。
在Angular应用程序中使用@NgModule装饰器实现模块;
@NgModule重要组成:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
上述代码由Angular构建工具默认生成。
@NgModule模块属性的释义:
-
declarations:在这里声明一些模块中要使用到的一些组件,指令,管道等 -
imports:导入一些模块,比如说我把所有的指令构成一个模块 我使用其中某些指令的时候,我可以选择导入整个指令模块。也可以导入一些通过npm install 安装的一些模块导入其中,才可以使用 -
providers:将本模块所有组件使用到的服务在这里注册 -
bootstrap:模块的启动组件,可以有多个 -
exports:导出组件、指令等,提供给其他模块的组件使用
-
schemas:不属于Angular的组件或者指令的元素或者属性都需要在这里进行声明
@NgModule常用模块
|
NgModule |
导入自 |
为何使用 |
|---|---|---|
|
|
当你想要在浏览器中运行应用时 |
|
|
|
当你想要使用 |
|
|
|
当要构建模板驱动表单时(它包含 |
|
|
|
当要构建响应式表单时 |
|
RouterModule |
@angular/router |
要使用路由功能,并且你要用到 |
|
|
当你要和服务器对话时 |
本文来自博客园,作者:菜鸟小辛,转载请注明原文链接:https://www.cnblogs.com/hashxin/p/15525107.html
浙公网安备 33010602011771号