Angular2 Http与RXJS Observable TypeError:this.http.get(...).map(...).catch不是函数

Jor*_*ngh 12 rxjs typescript angular

我有以下服务工作正常,直到今天我收到此错误

TypeError: this.http.get(...).map(...).catch is not a function. 
Run Code Online (Sandbox Code Playgroud)

当我调试这段代码时,它遇到了catch方法.

import { Test } from "./home.component";
import { Injectable }     from "@angular/core";
import { Inject } from "@angular/core";
import { Http , Response  } from "@angular/http";
import { Observable }     from "rxjs/Observable";

@Injectable()
export class HomeService {
   public constructor(@Inject(Http)  private http: Http) {}

   public getData (): Observable<Test []> {
        return this.http.get("./src/app/home/home-data.json")
            .map(this.extractData).catch(this.handleError);
    }

    public extractData(res: Response) {
        let body = res.json();
        return body.data || { };
    }

    public handleError (error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We"d also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : "Server error";
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
  }
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 26

看来catch操作符不是导入的.

您可以尝试像这样导入它:

import 'rxjs/add/operator/catch'
Run Code Online (Sandbox Code Playgroud)

或者更常见的是,如果您想要有更多可观察方法:

import 'rxjs/Rx';
Run Code Online (Sandbox Code Playgroud)

看到这个问题: