typescript 带有 RXJS Observable TypeError 的 Angular2 Http:this.http.get(...).map(...).catch 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38425825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:39:50  来源:igfitidea点击:

Angular2 Http with RXJS Observable TypeError: this.http.get(...).map(...).catch is not a function

typescriptangularrxjs

提问by Jorawar Singh

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

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

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

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

当我调试这段代码时,它会在涉及到 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);
    }
  }

回答by Thierry Templier

It seems that the catch operator isn't imported.

似乎没有导入 catch 运算符。

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:

或者更一般地说,如果您想为 observable 提供更多方法:

import 'rxjs/Rx';

See this question:

看到这个问题:

回答by Thilanka Ishara Gunathilake

I had the same issue but in my case the problem was, I had import Required modules several times in Module.ts

我遇到了同样的问题,但就我而言,问题是,我在 Module.ts 中多次导入了必需的模块

回答by Recev Yildiz

The 'O' character in 'rxjs/Observable' must be Upper Case.

'rxjs/ Observable' 中的 ' O' 字符必须是Upper Case

Specifying it in lower case 'o', will give you this and other spooky errors. This must be imported like this:

用小写的“ o”指定它,会给你这个和其他令人毛骨悚然的错误。这必须像这样导入:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

It took me an hour to find it. When using import, generally, the names are in lower case. This is the first time I'm seeing this. I hope it will help the others :)

我花了一个小时才找到它。使用 import 时,名称通常为小写。这是我第一次看到这个。我希望它会帮助其他人:)