Angular2提供自定义Http不起作用 [英] Angular2 Provide Custom Http Not Working

查看:104
本文介绍了Angular2提供自定义Http不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要一个全局空间来捕获http 401,403和500个响应.我查看了一些教程,并尝试了扩展http的方法.这是我的自定义HTTP(主要是从在线复制的)

We need a global space to capture http 401,403, and 500 responses. I looked at a few tutorials and tried an approach of extending http. Here is my custom HTTP (largely copied from online)

import { Http, ConnectionBackend, Request, RequestOptions, RequestOptionsArgs, Response, Headers} from '@angular/http';
import { Router} from '@angular/router';
import { Injectable} from '@angular/core';
import { Observable} from 'rxjs/Rx';

@Injectable()


export class CustomHttp extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router : Router) {
        super(backend, defaultOptions);
    }

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.request(url, options));
    }

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.get(url, options));
    }

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.post(url, body, this.getRequestOptionArgs(options)));
    }

    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
    }

    delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.delete(url, options));
    }

    getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
        if (options == null) {
            options = new RequestOptions();
        }
        if (options.headers == null) {
            options.headers = new Headers();
        }
        options.headers.append('Content-Type', 'application/json');
        return options;
    }

    intercept(observable: Observable<Response>): Observable<Response> {
        return observable.catch((err, source) => {
            console.log('CustomHttp Error status:  ' + err.status);
            return Observable.throw(err); 
        });
    }

}

这是我引导应用程序并尝试用实现替换默认HTTP的方法:

Here is how I bootstrap my app and try to replace default HTTP with my implementation:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { HTTP_PROVIDERS, Http, XHRBackend, RequestOptions} from '@angular/http';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';
import { APP_ROUTER_PROVIDERS  } from './app.routes';
import { provide, PLATFORM_DIRECTIVES} from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { CustomHttp } from './utils/http/customhttp';


    bootstrap(AppComponent, [
        APP_ROUTER_PROVIDERS,
        HTTP_PROVIDERS,
        provide(LocationStrategy, { useClass: HashLocationStrategy }),
        provide(PLATFORM_DIRECTIVES, { useValue: [ROUTER_DIRECTIVES], multi: true }),
        provide( Http, {
            useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, router: Router) => new CustomHttp(xhrBackend, requestOptions, router),
            deps: [XHRBackend, RequestOptions, Router]
        }),
    ]);

没有编译错误,但是当我尝试使用默认提供程序而不是我的实现发出一些http请求时.我之所以知道这一点,是因为我故意调用了一个返回500的webapi端点,并且未将其设置在catch子句中并编写了log语句.

The no compile errors but when I try to make some http requests its using the default provider and not my implementation. I know this because I purposefully call a webapi endpoint that returns a 500 and don't set into my catch clause and write a log statement.

这是我的js相关性:

 "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.4",

    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",

    "lodash": "4.13.1",
    "angular2-in-memory-web-api": "0.0.14",
    "bootstrap": "^3.3.6"
  }

我在这里想念什么胶水?

What glue am I missing here?

推荐答案

确保在某些组件上没有提供HTTP_PROVIDERSHttp,否则可能会改用它(取决于确切提供的位置)

Ensure you don't have HTTP_PROVIDERS or Http provided on some component, otherwise this might be used instead (depending on where exactly it is provided).

这篇关于Angular2提供自定义Http不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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