Created
June 8, 2017 13:20
-
-
Save chgc/84c0ea44307186420f5f3959a2470864 to your computer and use it in GitHub Desktop.
use Map 來儲存 service
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, Inject, Injector} from '@angular/core'; | |
import {serviceToken} from './app.module'; | |
import {IService} from './iservice'; | |
import {Service1Service} from './service1.service'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'app'; | |
private services = new Map<string, IService>(); | |
constructor(private injector: Injector) { | |
(<IService[]>injector.get(serviceToken)).forEach(s => { | |
this.services.set(s.kind, s); | |
}); | |
} | |
callFunction(kind: string): IService|undefined { | |
return this.services.get(kind); | |
} | |
callService(kind: string) { | |
const s = this.callFunction(kind); | |
console.log(s.log()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment