Angular 模块是带有 @NgModule 装饰器函数的类。 @NgModule接收一个元数据对象,该对象告诉 Angular 如何编译和运行模块代码。 它标记出该模块拥有的组件、指令和管道, 并把它们的一部分公开出去,以便外部组件使用它们。 它能够向应用的依赖注入器中添加服务提供商。html
每一个 Angular 应用都有一个根模块类。 按照约定,它的类名叫作AppModule,被放在app.module.ts文件中。bootstrap
src/app/app.module.ts (minimal) import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
下面范例AppComponent显示被绑定的标题:浏览器
src/app/app.component.ts (minimal) import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: '<h1>{{title}}</h1>', }) export class AppComponent { title = 'Minimal NgModule'; }
src/main.ts (dynamic) import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } platformBrowserDynamic().bootstrapModule(AppModule);
预编译器 (AoT - Ahead-Of-Time) 进行静态引导网络
src/main.ts (static) // The browser platform without a compiler import { platformBrowser } from '@angular/platform-browser'; // The app module factory produced by the static offline compiler import { AppModuleNgFactory } from './app/app.module.ngfactory'; // Launch with the app module factory. platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
src/app/title.component.html (ngIf) <p *ngIf="user"> <i>Welcome, {{user}}</i> <p>
src/app/app.module.ts (imports) imports: [ BrowserModule ],
若是有两个同名指令,都叫作HighlightDirective,该怎么办呢?咱们只要在 import 时使用as关键字来为第二个指令建立个别名就能够了。app
src/app/app.module.1b.ts import { HighlightDirective as ContactHighlightDirective } from './contact/highlight.directive';
当两个指令在同一个元素上争相设置颜色时,后声明的那个会胜出,由于它对 DOM 的修改覆盖了前一个ide
src/app/highlight.directive.ts import { Directive, ElementRef } from '@angular/core'; @Directive({ selector: '[highlight]' }) /** Highlight the attached element in gold */ export class HighlightDirective { constructor(el: ElementRef) { el.nativeElement.style.backgroundColor = 'gold'; console.log( `* AppRoot highlight called for ${el.nativeElement.tagName}`); } }
src/app/contact/highlight.directive.ts import { Directive, ElementRef } from '@angular/core'; @Directive({ selector: '[highlight], input' }) /** Highlight the attached element or an InputElement in blue */ export class HighlightDirective { constructor(el: ElementRef) { el.nativeElement.style.backgroundColor = 'powderblue'; console.log( `* Contact highlight called for ${el.nativeElement.tagName}`); } }
它们在技术上有两个显著的不一样点:函数
src/app/app.component.ts (v3 - Template) template: ` <app-title [subtitle]="subtitle"></app-title> <nav> <a routerLink="contact" routerLinkActive="active">Contact</a> <a routerLink="crisis" routerLinkActive="active">Crisis Center</a> <a routerLink="heroes" routerLinkActive="active">Heroes</a> </nav> <router-outlet></router-outlet> `
src/app/app-routing.module.ts import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; export const routes: Routes = [ { path: '', redirectTo: 'contact', pathMatch: 'full'}, { path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' }, { path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
src/app/app-routing.module.ts { path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' }, { path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' }
咱们添加SharedModule来存放这些公共组件、指令和管道,而且共享给那些须要它们的模块工具
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { AwesomePipe } from './awesome.pipe'; import { HighlightDirective } from './highlight.directive'; @NgModule({ imports: [ CommonModule ], declarations: [ AwesomePipe, HighlightDirective ], exports: [ AwesomePipe, HighlightDirective, CommonModule, FormsModule ] }) export class SharedModule { }
值得注意的有:性能
咱们能够把一些组件收集到单独的CoreModule中,而且只在应用启动时导入它一次,而不会在其它地方导入它code
模块的静态方法forRoot能够同时提供并配置服务,它接收一个服务配置对象,并返回一个ModuleWithProviders。这个简单对象具备两个属性:
根模块AppModule会导入CoreModule类并把它的providers添加到AppModule的服务提供商中,更精确的说法是,Angular 会先累加全部导入的提供商,而后才把它们追加到@NgModule.providers中, 这样能够确保咱们显式添加到AppModule中的那些提供商老是优先于从其它模块中导入的提供商
src/app/core/core.module.ts constructor (@Optional() @SkipSelf() parentModule: CoreModule) { if (parentModule) { throw new Error( 'CoreModule is already loaded. Import it in the AppModule only'); } }