angular – 如何在RC6中覆盖Http类?

前端之家收集整理的这篇文章主要介绍了angular – 如何在RC6中覆盖Http类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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类时遇到问题.有谁知道如何做到这一点?

语法有所改变,除此之外它应该仍然有效:
{ provide: Http,useFactory: (
            backend: XHRBackend,defaultOptions: RequestOptions) =>
            new CustomHttp(backend,RequestOptions]
    }
原文链接:https://www.f2er.com/angularjs/143354.html

猜你在找的Angularjs相关文章