Angular4 模块说明以及划分
Angular模块 (NgModule)Angular 模块是带有 @NgModule 装饰器函数的类。 @NgModule接收一个元数据对象,该对象告诉 Angular 如何编译和运行模块代码。 它标记出该模块拥有的组件、指令和管道, 并把它们的一部分公开出去,以便外部组件使用它们。 它可以向应用的依赖注入器中添加服务提供商。 每个 Angular 应用都有一个根模块类。 按照约定,它的类名叫做AppModule,被放在app.module.ts文件中。 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'; }
Angular编译(JIT和AOT)
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);
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关键字来为第二个指令创建个别名就可以了。 src/app/app.module.1b.ts import { HighlightDirective as ContactHighlightDirective } from './contact/highlight.directive'; 当两个指令在同一个元素上争相设置颜色时,后声明的那个会胜出,因为它对 DOM 的修改覆盖了前一个 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}`); } }
特性模块
它们在技术上有两个显著的不同点:
用路由器实现惰性 (lazy) 加载应用路由
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/hero/hero.module#HeroModule' }
RouterModule.forRoot和forChild方法
共享模块我们添加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 { } 值得注意的有:
重新导出其它模块
为什么 Service 没有被共享
核心 (Core) 模块我们可以把一些组件收集到单独的CoreModule中,并且只在应用启动时导入它一次,而不会在其它地方导入它 用 CoreModule.forRoot 配置核心服务模块的静态方法forRoot可以同时提供并配置服务,它接收一个服务配置对象,并返回一个ModuleWithProviders。这个简单对象具有两个属性:
根模块AppModule会导入CoreModule类并把它的providers添加到AppModule的服务提供商中,更精确的说法是,Angular 会先累加所有导入的提供商,然后才把它们追加到@NgModule.providers中, 这样可以确保我们显式添加到AppModule中的那些提供商总是优先于从其它模块中导入的提供商 禁止多次导入CoreModulesrc/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'); } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- shell – 递归检查具有可执行位集的文件
- angularjs – Angular-UI多个datepickers内幕表单控制器
- 在vim中打开一个目录
- 使用shell并使用Java API为HBase中的列族设置TTL
- Vim中最令人信服的命令是什么?
- scala – 如何告诉sbt-proguard包含java * .jars?
- scala – Spark case类 – 十进制类型编码器错误“无法从十
- 图像 – WindowsPhone 8和AngularJs – ng-src无法正常工作
- SMS(短讯服务中心)[二]-----webservice
- selenium-webdriver – Webdriver异常:“chrome无法访问