I am working with a basic ServiceBase
class that has Http
injected into its constructor:
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
export abstract class ServiceBase<T> {
constructor(private http: Http) {
}
protected getData(url: string): Promise<T> {
return this.http.get(url).toPromise()
.then(response => response.json() as T)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.log('An error occurred', error);
return Promise.reject(error.message || error);
}
}
The child class then extends the functionality of the base class:
@Injectable()
export class WeatherService extends ServiceBase<Weather> {
constructor(http: Http) {
super(http);
}
getWeather(): Promise<Weather> {
return this.getData('api/weather');
}
}
Currently, I have to inject Http
into each child class and pass it to the parent class using super(http);
. Is there a way to avoid this repetitive injection and pass-through? Since the child classes will be utilizing methods from the parent class which already includes the Http
service.