I want to override Http
with a custom class, say HttpClient
to do some operations before a request and after a response, however I don't want to have to remember to import that class instead of the platform http class.
I've been experimenting with doing this via the 'multi' providers, but I can't quite make it click.
Here's my override class:
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
@Injectable()
export class HttpClient {
private http: Http;
constructor(http: Http) {
this.http = http;
}
get(url) {
let headers = new Headers();
doSomethingToHeader(headers);
return this.http.get(url, {
headers: headers
});
}
}
And here's my main.ts
bootstrap(AppComponent, [
provide(HTTP_PROVIDERS, {useExisting: HTTP_PROVIDERS, multi: true}),
provide(HTTP_PROVIDERS, {useClass: HttpClient, multi: true})
]);
But when I try to call http.get() in my app, I get
ORIGINAL EXCEPTION: No provider for Http!
Any ideas? Am I going about this the wrong way?
Thanks!
UPDATE
I found this blog post. It pretty much describes what Gunter describes below. I'm accepting his answer.