如何在RC6中覆盖Http类? [英] How to override Http class in RC6?

查看:49
本文介绍了如何在RC6中覆盖Http类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CustomHttp类,并使用它为我的get请求添加标头:

I have CustomHttp class and I use it to add headers to my get requests:

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提供程序中:

In RC5, I added it to my AppModule providers like this:

provide (Http, {
            useFactory: (
                backend: XHRBackend,
                defaultOptions: RequestOptions) =>
                new CustomHttp(backend, defaultOptions),
            deps: [XHRBackend, RequestOptions]
        })

但是,在RC6中,不推荐使用@angular/core中的provide,并且在将CustomHttp类添加到AppModule提供程序时遇到了问题.有谁知道如何做到这一点?

But, in RC6, provide from @angular/core is deprecated and I'm having problems adding my CustomHttp class to AppModule providers. Does anyone have an idea how to do this?

推荐答案

语法有所变化,除了它仍然可以正常工作之外:

The syntax has change a bit, besides that it should still work the same:

    { provide: Http, 
        useFactory: (
            backend: XHRBackend,
            defaultOptions: RequestOptions) =>
            new CustomHttp(backend, defaultOptions),
        deps: [XHRBackend, RequestOptions]
    }

这篇关于如何在RC6中覆盖Http类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆