In my quest to learn Angular2, i came across the following situation:
- 1 base class, which uses the angular2/http dependency
- 1 inheriting class which extends the base class
How can I inject the angular2/http dependency in the base class's constructor, without having to pass http as a parameter when instantiating the inheriting class.
I don't use http in the inheriting class, so I don't want to see it there!
For example
// 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();
}
}
Ideally, I would just create a new http instance in the base class and use that like so let http = new Http();
, but that obviously doesn't work.