angular - “可观察”类型上不存在属性“catchError”>'
问题描述
从 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;
}
}
解决方案
这更多的是 rxjs 的变化。您需要熟悉可出租的运算符,但这里是您想要进行的代码更改...
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
observable 的函数中!
推荐阅读
- swagger - 如何将 Swagger 与 Nest.js API 和 SPA 之间共享的 DTO 一起使用?
- sql - 按名称分组的连续日期范围内的最小和最大日期
- c++ - CPU如何“投射”浮点x87(我认为)值?
- c++ - g++ 和双精度
- python-3.x - 如何在 Python 中对子类的更改最小的情况下稍微修改从父 __init__() 继承的属性?
- java - 从java中的文件中删除一个对象
- laravel - 如何使用 Laravel Dusk 或 WebDriverBy 在表格的“textarea”中输入文本?
- python - 如果元素相同,则用于查找索引元素的编号的 Python 列表索引
- swift - 如何在 SwiftUI 的不同视图中保留列表
- image - 用于内联图片或形状的 Word 2007 VBA 通配符