【问题标题】:Property 'catchError' does not exist on type 'Observable<HttpEvent<any>>''Observable<HttpEvent<any>>' 类型上不存在属性 'catchError'
【发布时间】:2018-11-16 17:47:38
【问题描述】:

从 angular 5 到 6(使用 angular 6 和 rxjs 6),在我的 linter 中出现以下两个错误。大家有什么想法,谢谢。

[ts] 'catchError' is declared but its value is never read.
[ts] Property 'catchError' does not exist on type 'Observable<HttpEvent<any>>'.
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';



@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(authReq)
      .catchError((error, caught) => {
        console.log('Error Occurred');
        console.log(error);
        return Observable.throw(error);
      }) as any;
  }
}

【问题讨论】:

标签: angular typescript rxjs observable rxjs6


【解决方案1】:

这更多的是 rxjs 的变化。您需要熟悉 lettable 运算符,但这里是您想要进行的代码更改...

import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';



@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(authReq)
      .pipe(catchError((error, caught) => {
        console.log('Error Occurred');
        console.log(error);
        return Observable.throw(error);
      })) as any;
  }
}

很简单吧!大部分 rxjs 操作符现在都传入了 observable 的 pipe 函数!

【讨论】:

  • Observable.throw -> throwError.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 2023-04-10
  • 2018-11-18
  • 2021-09-23
  • 2018-04-19
  • 1970-01-01
  • 2017-10-20
相关资源
最近更新 更多