Created
June 7, 2017 07:53
-
-
Save chgc/ea35f15d8ffc40342b12b4822eb1bd07 to your computer and use it in GitHub Desktop.
providers: 使用 multi 的情境
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, Injector} from '@angular/core'; | |
import {serviceToken} from './app.module'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'app'; | |
service: any[]; | |
constructor(private injector: Injector) { | |
this.service = <any[]>injector.get(serviceToken); | |
this.callFunction('service2'); | |
} | |
callFunction(kind: string) { | |
const idx = this.service.findIndex(x => x.kind === kind); | |
if (idx > -1) { | |
console.log(this.service[idx].log()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {InjectionToken, NgModule} from '@angular/core'; | |
import {BrowserModule} from '@angular/platform-browser'; | |
import {AppComponent} from './app.component'; | |
import {Service1Service} from './service1.service'; | |
import {Service2Service} from './service2.service'; | |
import {Service3Service} from './service3.service'; | |
export const serviceToken = new InjectionToken('serviceToken'); | |
@NgModule({ | |
declarations: [AppComponent], | |
imports: [BrowserModule], | |
providers: [ | |
{provide: serviceToken, useClass: Service1Service, multi: true}, | |
{provide: serviceToken, useClass: Service2Service, multi: true}, | |
{provide: serviceToken, useClass: Service3Service, multi: true}, | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
@Injectable() | |
export class Service1Service { | |
kind = 'service1'; | |
constructor() {} | |
log() { | |
return 'service1'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
@Injectable() | |
export class Service2Service { | |
kind = 'service2'; | |
constructor() {} | |
log() { | |
return 'service2'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
@Injectable() | |
export class Service3Service { | |
kind = 'service3'; | |
constructor() {} | |
log() { | |
return 'service3'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment