angular 模块的理解

  1. 前端模块化的理解
  2. Angular Module 有什么作用
  3. 如何定义一个Angular module @ngModule()装饰器的使用
  4. Angular 本身有哪些常用的Module
  5. 在工程中实际运用Module

Angular Module

前端模块化的发展

JavaScript脚本语言的推出处理浏览器人机交互,可随着web应用的交互复杂化,丰富化,脚本代码的可组织重用性、隔离性、可维护性、版本管理、依赖管理等问题就浮现出来,自然而然的javascript 就走上了模块化发展的道路。

最初的javascript模块化特性实现

最早使用 Script 标签、组织目录文件,闭包,利用对象模拟命名空间等方法来实现一个重用性,隔离性的特性

  1. var app = {};
  2. app.hello = {
  3. sayHi: function(){
  4. console.log('Hi');
  5. },
  6. sayHello: function(){
  7. console.log('Hello');
  8. }
  9. }

CommonJS模块化规范的制定

  • 上面的这些方法并不能很好的解决可组织重用性、隔离性、可维护性、版本管理、依赖管理这些问题,09年CommonJS 社区以此发展出了很多的模块化规范(CMD,AMD之类的)(node.js 基于CommonJs规范发布),这些规范以及加载库的提出解决了前端依赖管理等一些问题,
  • 模块化写法浏览器并不能直接执行, webpack 工具的出现解决打包编译的问题。 ```bash //hello.js function hello(){ this.sayHi = function(){ console.log( “Hello foo” ); }

    this.sayHello = function(){ console.log( “Hello bar” ); } }

module.exports.hello = hello;

var hello = require(“./hello”).hello, test = new hello(); test.hello()

  1. <a name="feCXt"></a>
  2. #### 原生语言层面的支持
  3. - ES6 中将Module作为标准模块化规范,ES6 module与CommonJS CMD 规范的差异???
  4. <a name="Jm3jC"></a>
  5. #### Angular Module
  6. - Angular 定义了@NgModule() 装饰器 ,为一个组件集声明了编译的上下文环境,(意味着angular Module 是angular 打包的最小单位)它专注于某个应用领域、某个工作流或一组紧密相关的能力,它可以将其组件和一组相关代码(如服务)关联起来,形成功能单元。
  7. - 像 JavaScript 模块一样,NgModule 也可以从其它 NgModule 中导入功能,并允许导出它们自己的功能供其它 NgModule 使用
  8. - 得益于angular module 的设计,配置了异步模块后,angular cli comper 会自动把模块切分成独立的chunk,其他的框架需要webpack 去配置代码分割
  9. <a name="LXSuI"></a>
  10. ### Angular @NgModule的使用
  11. ```typescript
  12. import { NgModule } from '@angular/core';
  13. import { BrowserModule } from '@angular/platform-browser';
  14. import { FormsModule } from '@angular/forms';
  15. import { AppRoutingModule } from './app-routing.module';
  16. import { AppComponent } from './app.component';
  17. import { LifecycleComponent } from "./lifecycle/lifecycle.component";
  18. import { MainComponent } from "./main/main.component";
  19. import { OnChangeComponent } from "./onChange/on-change.component";
  20. import { SetterComponent } from "./setter/setter.component";
  21. import { ViewChildComponent } from "./viewChild/view-child.component";
  22. import { MyInputComponent } from "./my-input/my-input.component";
  23. import { TemplateOutletComponent } from "./template-outlet/template-outlet.component";
  24. @NgModule({
  25. /**
  26. * 那些属于本 NgModule 的组件、指令、管道
  27. * */
  28. declarations: [
  29. AppComponent,
  30. MainComponent,
  31. LifecycleComponent,
  32. OnChangeComponent,
  33. SetterComponent,
  34. ViewChildComponent,
  35. MyInputComponent,
  36. TemplateOutletComponent
  37. ],
  38. /**
  39. * 用来导入外部模块
  40. * */
  41. imports: [
  42. BrowserModule,
  43. AppRoutingModule,
  44. FormsModule
  45. ],
  46. /**
  47. * 需要使用的 Service 都放在这里
  48. * */
  49. providers: [],
  50. /**
  51. * 应用的主视图,称为根组件。它是应用中所有其它视图的宿主。只有根模块才应该设置这个 bootstrap 属性
  52. * */
  53. bootstrap: [ AppComponent ]
  54. })
  55. export class AppModule {
  56. }
  57. /**
  58. * 应用的主视图,称为根组件。它是应用中所有其它视图的宿主。只有根模块才应该设置这个 bootstrap 属性
  59. * */

Angular 本身有哪些常用的Module