gpt4 book ai didi

angular - 类型 'catchError' 上不存在属性 'Observable>'

转载 作者:搜寻专家 更新时间:2023-10-30 21:05:19 26 4
gpt4 key购买 nike

从 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 函数中!

关于angular - 类型 'catchError' 上不存在属性 'Observable<HttpEvent<any>>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50730598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com