角度6:类型'Observable< Response>'上不存在属性'catch'吗? [英] Angular 6: Property 'catch' does not exist on type 'Observable<Response>'?

查看:71
本文介绍了角度6:类型'Observable< Response>'上不存在属性'catch'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用升级到 Angular 6 .我正在从 Angular 4 升级,但是下面的代码在Angular 6中导致错误,在Angular 4中可以正常工作.

I am upgrading my app to Angular 6. I am upgrading from Angular 4, but the code below is causing errors in Angular 6, where it worked fine in Angular 4.

我得到的错误:

属性"of"在"typeof Observable"类型上不存在

Property 'of' does not exist on type 'typeof Observable'

错误:可观察"类型不存在属性捕获"

Error: Property 'catch' does not exist on type 'Observable'

我应该如何解决这些错误?

How should I resolve these errors?

  private authInterceptor(observable: Observable<Response>): Observable<Response> {
    return observable.catch((error, source) => {
      if (error.status == 401) {
        this.router.navigateByUrl('/login');
        return Observable.of();
      } else {
        return Observable.throw(error);
      }
    });
  }

推荐答案

由于您标记了问题rxjs6,所以我假设升级到Angular 6包括升级到rxjs6.在那种情况下,它不起作用,因为可观察对象上的方法现在是独立的运算符,您可以使用pipe()对其进行应用.此外,进口也发生了变化.有关更多详细信息,请参见迁移指南.

Since you tagged your question rxjs6, I'm assuming the upgrade to Angular 6 includes an upgrade to rxjs6. In that case, it's not working because methods on the observable object are now standalone operators that you can apply using pipe(). Also, imports have changed. See the migration guide for more details.

使用rxjs6时,它应该看起来像这样:

With rxjs6 it should look something like this:

import { Observable, EMPTY, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

private authInterceptor(observable: Observable<Response>): Observable<Response> {
   return observable.pipe(
       catchError( err => {
            if (err.status == 401) {
                this.router.navigateByUrl('/login');
                return EMPTY;
            } else {
                return throwError(err);
            }
       })
   );
 }

这篇关于角度6:类型'Observable&lt; Response&gt;'上不存在属性'catch'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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