现代方法是实现自定义Http
客户端。
export function getCustomHttp(xhrBackend: XHRBackend, requestOptions: RequestOptions) {
return new CustomHttp(xhrBackend, requestOptions);
}
export class CustomHttp extends Http {
public constructor(backend: XHRBackend, private defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
public request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
url = 'https://www.customURL.com/' + url; // Of course, you'd pull this from a config
return super.request(url, options);
}
}
然后你只需修改你app.module
的如下:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoute,
RouterModule
],
providers: [
HttpModule,
{
provide: Http,
useFactory: getCustomHttp,
deps: [XHRBackend, RequestOptions]
}
],
bootstrap: [AppComponent]
})
export class AppModule { }