我有CustomHttp类,我用它来为我的get请求添加标头:
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import { RequestOptionsArgs,RequestOptions,ConnectionBackend,Http,Request,Response,Headers } from "@angular/http"; @Injectable() export class CustomHttp extends Http { headers: Headers = new Headers({ 'Something': 'Something' }); options1: RequestOptions = new RequestOptions({ headers: this.headers }); constructor(backend: ConnectionBackend,defaultOptions: RequestOptions) { super(backend,defaultOptions); } get(url: string,options?: RequestOptionsArgs) { console.log('Custom get...'); return super.get(url,this.options1).catch(err => { console.log(err); if (err.status === 404) { console.log('404 error'); return Observable.throw(err); } }); } }
在RC5中,我将它添加到我的AppModule提供程序中,如下所示:
provide (Http,{ useFactory: ( backend: XHRBackend,defaultOptions: RequestOptions) => new CustomHttp(backend,defaultOptions),deps: [XHRBackend,RequestOptions] })
但是,在RC6中,@ angular / core的提供已被弃用,我在向AppModule提供程序添加CustomHttp类时遇到问题.有谁知道如何做到这一点?
语法有所改变,除此之外它应该仍然有效:
原文链接:https://www.f2er.com/angularjs/143354.html{ provide: Http,useFactory: ( backend: XHRBackend,defaultOptions: RequestOptions) => new CustomHttp(backend,RequestOptions] }