我有一个使用服务的组件。该组件看起来像这样:
@Component({
moduleId: module.id,
selector: 'test',
providers: [HTTP_PROVIDERS, TestService]
})
export class TestComponent implements OnInit {
constructor(private _testService:TestService) {
}
如您所见,我HTTP_PROVIDERS在组件中添加了提供程序。这很有效,因为 DI 现在知道这些http类了。但是,TestService真正使用Http课程的是我的,而不是我的TestComponent.
@Injectable()
export class TestService {
constructor(private _http:Http) {
}
我觉得既然是使用Http类的服务,它应该是包含提供者本身的服务。他们TestComponent不知道供应商TestService需要什么。
由于服务类没有那个组件装饰器,我不确定如何实际向它添加提供程序。如何将提供者添加到Service课程中?