【发布时间】:2016-07-15 12:31:55
【问题描述】:
我有三个服务和一个组件:
root.serviceabstarct-child.serviceextend-child.serviceapp.component
root.service 使用依赖注入注入abstarct-child.service,如下所示:
import {Injectable} from 'angular2/core';
import {AbstractChildService} from './abstarct-child.service';
@Injectable()
export class RootService {
constructor(private _abstractChildService: AbstractChildService) {}
UseChildServiceDoSomethingFunction(){}
}
abstarct-child.service 看起来像这样:
import {Injectable} from 'angular2/core';
@Injectable()
export abstract class AbstractChildService {
abstract doSomething(): any ;
}
extend-child.service 看起来像这样:
import {Injectable} from 'angular2/core';
import {AbstractChildService} from './abstarct-child.service';
@Injectable()
export class ExtendChildService extends AbstractChildService{
constructor(private _num: number) { super(); }
doSomething() : any { return true; }
}
app.component 提供 root.service 和 extend-child.service as abstract-child.service 就像这样:
import {Component} from 'angular2/core';
import {Input} from 'angular2/core';
import {provide} from 'angular2/core';
import {RootService} from './root.service';
import {AbstractChildService} from './abstarct-child.service';
import {ExtendChildService} from './extend-child.service';
@Component({
selector: 'app',
providers: [provide(AbstractChildService, { useClass: ExtendChildService, useValue: this.num})]
})
export class AppComponent {
@Input('given_Num') num: number;
}
我想知道如何向extend-child.service 注入一个对象,在本例中是app.component 的this.num?
谁能发现我做错了什么?
谢谢。
【问题讨论】:
标签: typescript angular angular2-directives angular2-services