本文介绍了类型'Observable< HttpEvent< any>>'上不存在属性'catchError'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从角度5转到6(使用角度6和rxjs 6),在我的短毛绒中出现以下两个错误.任何人都有任何想法,请谢谢.

Went from angular 5 to 6(using angular 6 & rxjs 6), getting the following two errors in my linter. Anybody have any ideas, please and thank you.

[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;
  }
}

推荐答案

这更多是rxjs的更改.您将要熟悉可操作的运算符,但是这里是您要进行的代码更改...

This is more of a change with rxjs. You'll want to familiarize yourself with lettable operators, but heres the code change you'll want to make...

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运算符都传递给了可观察对象的pipe函数!

Pretty easy right! Most of the rxjs operators are now passed into the pipe function of the observable!

这篇关于类型'Observable&lt; HttpEvent&lt; any&gt;&gt;'上不存在属性'catchError'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:03