RXJS 6:新版本的HttpInterceptor [英] RXJS 6: new version of HttpInterceptor
问题描述
我正在将 rxjs_compat
添加到我的项目中,以便迁移到 v6 的库.
然而,用于全局错误处理的现有 HttpInterceptor
不再编译.不知道该去哪里.各种都试过了.尝试过的所有方法都出现类型不匹配.
import { Injectable } from "@angular/core";进口 {HttpEvent,Http拦截器,HttpHandler,请求,HttpResponse,HttpErrorResponse来自@angular/common/http";import { Observable, of, empty } from "rxjs";从ngx-toastr"导入 { ToastrService };从../../environments/environment"导入{环境};import { catchError, map } from "rxjs/operators";@Injectable()导出类 HttpErrorInterceptor 实现 HttpInterceptor {构造函数(私有 toastr:ToastrService){}截距(请求:HttpRequest,下一个:HttpHandler): Observable>{返回 next.handle(request).pipe(catchError(err => of(HttpErrorResponse)),地图(错误 => {让消息:字符串;this.toastr.error(`${message}`, "应用程序错误");return Observable.empty>();}));}}
<块引用>
src/app/shared/http-error-interceptor.ts(26,27): 错误 TS2339:类型typeof Observable"上不存在属性empty".
empty
现在是一个常量,但不接受类型,因此也不起作用.在升级说明 中也找不到太多内容>
编辑
虽然有趣的是编译:
return Observable.of>();
从'rxjs'导入{EMPTY};
用
替换返回Observable.empty()
return EMPTY;
I am in the process of adding rxjs_compat
to my project in order to move to v6 of libraries.
However the existing HttpInterceptor
for global error handling no longer compiles. Not sure where to go with it. Tried all sorts. Getting type mismatches with everything tried.
import { Injectable } from "@angular/core";
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpResponse,
HttpErrorResponse
} from "@angular/common/http";
import { Observable, of, empty } from "rxjs";
import { ToastrService } from "ngx-toastr";
import { environment } from "../../environments/environment";
import { catchError, map } from "rxjs/operators";
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
constructor(private toastr: ToastrService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => of(HttpErrorResponse)),
map(err => {
let message: string;
this.toastr.error(`${message}`, "Application Error");
return Observable.empty<HttpEvent<any>>();
})
);
}
}
src/app/shared/http-error-interceptor.ts(26,27): error TS2339: Property 'empty' does not exist on type 'typeof Observable'.
empty
is now a constant, but doesn't accept a type, so that does not work either. Also could not find much in the upgrade notes
EDIT
although interestingly this compiles:
return Observable.of<HttpEvent<any>>();
import {EMPTY} from 'rxjs';
Replace return
Observable.empty()
withreturn EMPTY;
这篇关于RXJS 6:新版本的HttpInterceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!