Angular 4 Http 拦截器 : next. handle(...).do 不是函数

标签 angular angular-http-interceptors

我创建这个 HTTPInterceptor 是为了能够更好地处理 http 错误,在我执行 git pull 并运行 npm install 之前它运行良好。

这是我的代码:

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from "rxjs";
import {ToasterService} from "angular2-toaster";

@Injectable()
export class GobaeInterceptor implements HttpInterceptor {
    constructor(private toasterService: ToasterService){
    }
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req)
            .do(event => {
                if (event instanceof HttpResponse) {
                    let response = event.body;
                    if(response.Error){
                        this.toasterService.pop('error', 'Error '+response.Code, response.Message);
                    }
                }
            });
    }
}

这是我得到的错误:

TypeError: next.handle(...).do is not a function at GobaeInterceptor.webpackJsonp.../../../../../src/app/services/gobae.interceptor.ts.GobaeInterceptor.intercept (gobae.interceptor.ts:12) at HttpInterceptorHandler.webpackJsonp.../../../common/@angular/common/http.es5.js.HttpInterceptorHandler.handle (

最近是否有可能影响我的代码的更改?我现在可以做什么来在我的拦截器上“捕获”http 响应?

最佳答案

抛出此错误是因为您缺少 do 运算符。下面导入并使用 do 运算符修补可观察对象。

import 'rxjs/add/operator/do';

默认情况下,RxJs 并未捆绑所有运算符函数以减小库大小。您需要导入要单独使用的运算符。

关于Angular 4 Http 拦截器 : next. handle(...).do 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45846662/

相关文章:

javascript - ngOnDestroy 未在页面重新加载时触发

angular - 如何将 Angular 组件包裹在动态组件周围

typescript - Angular 2 Typescript 编译错误 TS2304?

angularjs - Angular JS - 向所有 GET/POST 请求添加自定义 header

angular - 如何以编程方式将 ionic 按钮添加到 Ionic 导航栏? ( ionic 2、 Angular 2)

Angular 2 路由器链接激活,两个链接激活

angular - 当 rxjs throwError 重新抛出 http 错误响应时,自定义全局错误处理程序未命中

angular - 如何删除在HTTP拦截器中设置的内容类型以以 Angular 4上传文件

angularjs - 无法读取 $httpInterceptor 中未定义的属性 'push'

Angular 5 HttpInterceptor 错误处理首先调用调用者的错误处理