Angular 4 依赖注入教程之二 组件服务注入git
Angular 4 依赖注入教程之四 FactoryProvider的使用typescript
Angular 4 依赖注入教程之六 Injectable 装饰器bootstrap
Angular 4 依赖注入教程之七 ValueProvider的使用segmentfault
本系列教程的开发环境及开发语言:app
在 Angular 中咱们经过如下方式建立一个简单的组件:
@Component({ selector: 'app-root', template: ` <h1>{{title}}</h1> ` }) export class AppComponent { title: string = 'App Works'; }
在 Angular 中咱们经过如下方式建立一个简单的服务:
export class DataService { getData() { return ['Angular', 'React', 'Vue']; } }
介绍完基础知识,接下来咱们来建立一个新的组件 - HeroComponent
,它用来显示英雄的信息,具体实现以下:
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-hero', template: ` <ul> <li *ngFor="let hero of heros"> ID: {{hero.id}} - Name: {{hero.name}} </li> </ul> ` }) export class HeroComponent implements OnInit { heros: Array<{ id: number; name: string }>; ngOnInit() { this.heros = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' } ]; } }
在 HeroComponent
组件中,咱们在 ngOnInit
钩子中进行数据初始化,而后利用 ngFor
指令来显示英雄列表的信息。建立完 HeroComponent
组件,咱们要来验证一下该组件的功能。首先在 AppModule
中导入 HeroComponent
组件,具体以下:
import { HeroComponent } from './hero/hero.component'; @NgModule({ declarations: [ AppComponent, HeroComponent ], ... }) export class AppModule { }
而后更新一下 AppComponent
组件,具体以下:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <app-hero></app-hero> ` }) export class AppComponent {}
若是不出意外的话,访问 http://localhost:4200/
页面,您将看到以下信息:
ID: 11 - Name: Mr. Nice ID: 12 - Name: Narco ID: 13 - Name: Bombasto ID: 14 - Name: Celeritas ID: 15 - Name: Magneta
难道一切就这么结束了,No! No!别忘记了咱们这节课的主题是介绍如何在组件中注入服务。在目前的 HeroComponent
组件,咱们的英雄列表信息是固定的,在实际的开发场景中,通常须要从远程服务器获取相应的信息。但咱们暂不考虑这个问题,假设另一个组件也须要利用一样的英雄列表信息,那咱们要怎么办,难道直接上 "终极绝招" - Copy && Paste
。固然这是 "终极绝招",岂能随便使用 (不怕被群殴的话,请自便哈)。
针对上面提到的问题,理想的方式是建立一个 HeroService
服务,从而实现数据共享。说干就干,咱们立刻来建立 HeroService
服务,具体以下:
export class HeroService { heros: Array<{ id: number; name: string }> = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' } ]; getHeros() { return this.heros; } }
在 HeroService
服务中,咱们定义了一个 heros
属性和一个 getHeros()
方法:
heros - 用于保存英雄的列表信息
getHeros() - 用于获取英雄的列表信息
建立完 HeroService
服务后,接下来咱们来介绍如何在组件中使用 HeroService
服务。
组件中使用 HeroService
服务,主要分为三个步骤:
导入 HeroService
服务
import { HeroService } from '../hero.service';
声明 HeroService
服务
@Component({ selector: 'app-hero', ... providers: [HeroService] })
注入 HeroService
服务
export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } }
完整代码以下:
import { Component, OnInit } from '@angular/core'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero', template: ` <ul> <li *ngFor="let hero of heros"> ID: {{hero.id}} - Name: {{hero.name}} </li> </ul> `, providers: [HeroService] }) export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } heros: Array<{ id: number; name: string }>; ngOnInit() { this.heros = this.heroService.getHeros(); } }
看到 providers: [HeroService]
这一行,相信有一些读者会有一些困惑,由于他们多是按照下面的方式去配置 HeroService
服务。
@NgModule({ declarations: [ AppComponent, HeroComponent ], ... providers: [HeroService], bootstrap: [AppComponent] }) export class AppModule { }
固然两种方式不会影响,咱们最终要实现的功能,但这两种方式确定是有区别的,但愿有兴趣的读者,去思考一下哈。在多数场景下,推荐在 NgModule
的 Metadata 信息中配置相应的服务。
HeroService
,在 HeroComponent
组件类的构造函数中还得进行类型声明?import { HeroService } from '../hero.service'; export class HeroComponent implements OnInit { constructor(private heroService: HeroService) { } }
其实在 @NgModule({...})
或 @Component({...})
Metadata 中咱们只是配置 Provider
的相关信息,即告诉 Angular DI (依赖注入) 系统,如何建立根据配置的 provider
信息,建立相应的依赖对象。而在 HeroComponent
组件类中,咱们经过构造注入的方式去告诉 Angular DI 系统,咱们须要的依赖对象类型。