我想Http
用一个自定义类覆盖,比如说HttpClient
在请求之前和响应之后做一些操作,但是我不想记住导入那个类而不是平台 http 类。
我一直在尝试通过“多”供应商来做这件事,但我不能让它点击。
这是我的覆盖类:
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
});
}
}
这是我的 main.ts
bootstrap(AppComponent, [
provide(HTTP_PROVIDERS, {useExisting: HTTP_PROVIDERS, multi: true}),
provide(HTTP_PROVIDERS, {useClass: HttpClient, multi: true})
]);
但是当我尝试在我的应用程序中调用 http.get() 时,我得到了
ORIGINAL EXCEPTION: No provider for Http!
有任何想法吗?我会以错误的方式解决这个问题吗?
谢谢!
更新
我找到了这篇博文。它几乎描述了 Gunter 在下面描述的内容。我接受他的回答。