Javascript 'Observable<any>' 类型上不存在属性 'catch'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37073705/
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-08-23 19:51:33  来源:igfitidea点击:

Property 'catch' does not exist on type 'Observable<any>'

javascriptangulartypescriptrxjs

提问by BrianRT

On the Angular 2 documentation page for using the Http service, there is an example.

在使用 Http 服务的 Angular 2 文档页面上,有一个示例。

getHeroes (): Observable<Stuff[]> {
  return this.http.get(this.url)
                  .map(this.extractData)
                  .catch(this.handleError);
}

I cloned the angular2-webpack-starterproject and added the above code myself.

我克隆了angular2-webpack-starter项目并自己添加了上面的代码。

I imported Observableusing

Observable使用导入

import {Observable} from 'rxjs/Observable';

I'm assuming the properties Observableare imported as well (.mapworks). Looked at the changelog for rxjs.beta-6 and nothing is mentioned about catch.

我假设属性Observable也被导入(.map有效)。查看了 rxjs.beta-6 的变更日志,没有提及catch.

回答by Thierry Templier

Warning: This solution is deprecated since Angular 5.5, please refer to Trent's answer below

警告:此解决方案自 Angular 5.5 起已弃用,请参考下面 Trent 的回答

=====================

======================

Yes, you need to import the operator:

是的,您需要导入运算符:

import 'rxjs/add/operator/catch';

Or import Observablethis way:

或以Observable这种方式导入:

import {Observable} from 'rxjs/Rx';

But in this case, you import all operators.

但在这种情况下,您导入所有运算符。

See this question for more details:

有关更多详细信息,请参阅此问题:

回答by Trent

With RxJS 5.5+, the catchoperator is now deprecated. You should now use the catchErroroperator in conjunction with pipe.

在 RxJS 5.5+ 中,该catch运算符现已弃用。您现在应该将catchError运算符与 结合使用pipe

RxJS v5.5.2 is the default dependency version for Angular 5.

RxJS v5.5.2 是 Angular 5 的默认依赖版本。

For each RxJS Operator you import, including catchErroryou should now import from 'rxjs/operators' and use the pipe operator.

对于您导入的每个 RxJS 运算符,包括catchError您现在应该从 'rxjs/operators' 导入并使用管道运算符。

Example of catching error for an Http request Observable

为 Http 请求 Observable 捕获错误的示例

import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
...

export class ExampleClass {
  constructor(private http: HttpClient) {
    this.http.request(method, url, options).pipe(
      catchError((err: HttpErrorResponse) => {
        ...
      }
    )
  }
  ...
}

Notice here that catchis replaced with catchErrorand the pipeoperator is used to compose the operators in similar manner to what you're used to with dot-chaining.

请注意,这里catchcatchErrorpipe运算符替换了运算符,以类似于您习惯使用点链的方式组合运算符。



See the rxjs documentation on pipable(previously known as lettable) operators for more info.

查看该rxjs文档pipable(以前称为可出租获取更多信息)运营商。

回答by Prince Babbar

In angular 8:
for catch:
import { catchError } from 'rxjs/operators';

for throw:
import { Observable, throwError } from 'rxjs';

and code should be written like this.

getEmployees(): Observable<IEmployee[]> {
    return this.http.get<IEmployee[]>(this.url).pipe(catchError(this.erroHandler));
  }

  erroHandler(error: HttpErrorResponse) {
    return throwError(error.message || 'server Error');
  }