<分区>
在学习Angular2的过程中,我遇到了以下情况:
- 1 个基类,它使用 angular2/http 依赖项
- 1个扩展基类的继承类
如何在基类的构造函数中注入(inject) angular2/http 依赖项,而不必在实例化继承类时将 http 作为参数传递。
我不在继承类中使用http,所以我不想在那里看到它!
例如
// base class
class CRUDService {
// http is used in this service, so I need to inject it here
constructor(@Inject(Http) http) {}
findAll() {
return this.http.get('http://some.api.com/api/some-model');
}
}
// inheriting class
class ThingService extends CRUDService {
constructor() {
// because we extend the base class, we need to call super()
// this will fail since it expects http as a parameter
// BUT i don't use http in this class, I don't want to inject it here
super();
}
}
理想情况下,我会在基类中创建一个新的 http 实例并像这样使用它 let http = new Http();
,但这显然行不通。