简体   繁体   English

&#39;Observable&#39; 类型不存在属性 &#39;catch&#39;<any> &#39;

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

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-starter project and added the above code myself.我克隆了angular2-webpack-starter项目并自己添加了上面的代码。

I imported Observable using我使用导入Observable

import {Observable} from 'rxjs/Observable';

I'm assuming the properties Observable are imported as well ( .map works).我假设Observable的属性也被导入( .map有效)。 Looked at the changelog for rxjs.beta-6 and nothing is mentioned about catch .查看 rxjs.beta-6 的更新日志,没有提到catch

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 Observable this way:或者以这种方式导入Observable

import {Observable} from 'rxjs/Rx';

But in this case, you import all operators.但在这种情况下,您导入所有运算符。

See this question for more details:有关更多详细信息,请参阅此问题:

With RxJS 5.5+, the catch operator is now deprecated.在 RxJS 5.5+ 中,现在不推荐使用catch运算符。 You should now use the catchError operator in conjunction with pipe .您现在应该将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 catchError you 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 catch is replaced with catchError and the pipe operator is used to compose the operators in similar manner to what you're used to with dot-chaining.请注意, catchcatchError替换, pipe运算符用于以与您习惯于使用点链接的方式类似的方式组合运算符。


See the rxjs documentation on pipable (previously known as lettable ) operators for more info.查看该rxjs文档pipable (以前称为可出租获取更多信息)运营商。

In angular 8:在角度 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');
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性&#39;mergeMap&#39;在类型&#39;Observable上不存在 <any> “ - Property 'mergeMap' does not exist on type 'Observable<any>' 属性“ catch”在类型“ PromiseLike”上不存在 <any> &#39; - Property 'catch' does not exist on type 'PromiseLike<any>' 错误:类型&#39;OperatorFunction &lt;{},{} |属性&#39;subscribe&#39;不存在 可观察的 <any> &gt; - Error: property 'subscribe' does not exist on type 'OperatorFunction<{}, {} | Observable<any>> Observable 类型上不存在属性管道 - Property pipe does not exist on type Observable “Observable”类型上不存在属性“过滤器”<Event> &#39; - Property 'filter' does not exist on type 'Observable<Event>' Observable 类型上不存在属性“then” <querysnapshot<documentdata> &gt; </querysnapshot<documentdata> - Property 'then' does not exist on type Observable<QuerySnapshot<DocumentData>> 类型“可观察”不存在属性“ toPromise” <Response> &#39;并且参数&#39;response&#39;隐式具有&#39;any&#39;类型 - Property 'toPromise' does not exist on type 'Observable<Response>' And Parameter 'response' implicity has an 'any' type Angular 6:“typeof Observable”类型上不存在“fromEvent”属性 - Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable' 类型 'void | 上不存在属性 'subscribe' 可观察的<user> '。 ts(2339)</user> - Property 'subscribe' does not exist on type 'void | Observable<User>'. ts(2339) 类型'Observable <{}>'上不存在属性'update'。 Firebase 5 AngularFire2 5 - Property 'update' does not exist on type 'Observable<{}>'. Firebase 5 AngularFire2 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM