【发布时间】:2016-07-10 07:37:26
【问题描述】:
我有一个使用服务的组件。该组件看起来像这样:
@Component({
moduleId: module.id,
selector: 'test',
providers: [HTTP_PROVIDERS, TestService]
})
export class TestComponent implements OnInit {
constructor(private _testService:TestService) {
}
如您所见,我在组件中添加了HTTP_PROVIDERS 提供程序。这很有效,因为 DI 现在知道 http 类。然而,真正使用Http 类的是我的TestService,而不是我的TestComponent。
@Injectable()
export class TestService {
constructor(private _http:Http) {
}
我觉得既然是使用Http类的服务,那么它本身应该是包含提供者的服务。 TestComponent 不知道 TestService 需要什么提供者。
由于服务类没有该组件装饰器,我不确定如何实际向它添加提供程序。如何将提供程序添加到 Service 类?
【问题讨论】:
标签: javascript angular angular2-services angular2-providers