Angular学习(1)——Angular模块化

Angular模块化

模块是组织应用和使用外部库扩展应用的最佳途径

NgModule 把组件、指令和管道打包成内聚的功能块,每个模块聚焦于一个特性区域、业务领域、工作流或通用工具。

模块还可以把服务加到应用中。 这些服务可能是内部开发的(比如你自己写的),或者来自外部的(比如 Angular 的路由和 HTTP 客户端)。

基本模块剖析

当你使用 Angular CLI 命令 ng new 生成一个应用时,其默认的 AppModule 是这样的:

// imports
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// @NgModule decorator with its metadata
@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: []
})
export class AppModule {}

@NgModule 装饰器表明 AppModule 是一个 NgModule 类。 @NgModule 获取一个元数据对象,它会告诉 Angular 如何编译和启动本应用。

  • declarations(可声明对象表) —— 那些属于本 NgModule 的组件、指令、管道。用于指示该模块的内部编译
  • imports(导入表) —— 导入的其他/第三方模块,之后才能在本模块中使用。
  • exports(导出表) —— 那些能在其它模块的组件模板中使用的可声明对象的子集。
  • providers —— 可在本模块中使用的可注入对象。
  • bootstrap(应用的主视图) —— 根组件,又是程序的入口组件,Angular创建并插入index.html宿主页面。它是应用中所有其它视图的宿主。只有根模块才应该设置这个 bootstrap 属性。
  • entryComponents —— The set of components to compile when this NgModule is defined, so that they can be dynamically loaded into the view. For each component listed here, Angular creates a ComponentFactory and stores it in the ComponentFactoryResolver.

常用模块

NgModule导入自为何使用
BrowserModule@angular/platform-browser当你想要在浏览器中运行应用时
CommonModule@angular/common当你想要使用 NgIfNgFor
FormsModule@angular/forms当要构建模板驱动表单时(它包含 NgModel
ReactiveFormsModule@angular/forms当要构建响应式表单时
RouterModule@angular/router要使用路由功能,并且你要用到 RouterLink,.forRoot().forChild()
HttpClientModule@angular/common/http当你要和服务器对话时

参考

NgModules官方简介

模块(Ngmodule)简介

Angular7入门辅助教程(一)——Angular模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值