带有RXJS Observable TypeError的Angular2 Http:this.http.get(...).map(...).catch不是函数 [英] Angular2 Http with RXJS Observable TypeError: this.http.get(...).map(...).catch is not a function

查看:90
本文介绍了带有RXJS Observable TypeError的Angular2 Http:this.http.get(...).map(...).catch不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在跟踪服务,直到今天我收到此错误为止,该服务都可以正常运行

I have following Service which was working fine until today i got this error

TypeError: this.http.get(...).map(...).catch is not a function. 

当我调试此代码时,它在捕获方法时崩溃.

When I'm debugging this code it crashes when it comes to catch method.

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);
    }
  }

推荐答案

似乎未导入catch运算符.

It seems that the catch operator isn't imported.

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

You could try to import it like this:

import 'rxjs/add/operator/catch'

或更笼统地说,如果您想要更多的可观察方法:

Or more generally this if you want to have more methods for observables:

import 'rxjs/Rx';

看到这个问题:

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