Hi I want to ask about create custom http class to handle base url and set default timeout.
嗨,我想问一下创建自定义http类来处理基本URL并设置默认超时。
I'm already implement this to myhttp class:
我已经在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);
}
}
and this is my 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));
but it doesn't work, Can anyone please guide?
但它不起作用,有人可以指导吗?
1 个解决方案
#1
0
Hei, I already found the solution.
嘿,我已经找到了解决方案。
The problem is because I'm provide HTTP_PROVIDERS twice
问题是因为我提供了两次HTTP_PROVIDERS
- in main.ts
- 在main.ts
- app.component.ts
- app.component.ts
So if you provide twice the custom http will be overide again with original http.
因此,如果您提供两次自定义http将再次使用原始http。
#1
0
Hei, I already found the solution.
嘿,我已经找到了解决方案。
The problem is because I'm provide HTTP_PROVIDERS twice
问题是因为我提供了两次HTTP_PROVIDERS
- in main.ts
- 在main.ts
- app.component.ts
- app.component.ts
So if you provide twice the custom http will be overide again with original http.
因此,如果您提供两次自定义http将再次使用原始http。