嗨,我想问关于创建自定义http类来处理基本url和设置默认超时。
我已经实现了这个myhttp类:
import { Injectable } from '@angular/core';
import { Http, Request, RequestOptions, ConnectionBackend, RequestMethod, RequestOptionsArgs, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Configuration } from '../config/configuration';
import { PageNavigationService } from './page-navigation.service';
@Injectable()
export class MyHttp extends Http {
serviceBase: string;
timeout: number;
constructor(
backend: ConnectionBackend,
defaultOptions: RequestOptions,
private _pageNavigationService: PageNavigationService) {
super(backend, defaultOptions);
this.serviceBase = Configuration.SERVICE.BASE_URL;
this.timeout = Configuration.SERVICE.TIME_OUT;
console.log('get: ' + this.serviceBase);
}
}
这是我的main。ts
bootstrap(AppComponent,
[
appRouterProviders,
HTTP_PROVIDERS,
PageNavigationService,
{
provide: Http,
useFactory: (backend: XHRBackend,
defaultOptions: RequestOptions,
_pageNavigationService: PageNavigationService) => new MyHttp(backend, defaultOptions, _pageNavigationService),
deps: [XHRBackend, RequestOptions, PageNavigationService]
}
]).catch(err => console.error(err));
但它不工作,谁可以指导?
嘿,我已经找到解决办法了。
问题是因为我提供了两次HTTP_PROVIDERS
- 在main.ts
- app.component.ts
所以如果你提供两次自定义http将再次覆盖原始http