我正在将rxjs_compat添加到我的项目中,以便移至v6库.
但是,用于全局错误处理的现有HttpInterceptor不再编译.不知道去哪里.尝试了各种.使类型与所有尝试过的都不匹配.
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):错误TS2339: 属性'empty'在类型'typeof Observable'上不存在.
empty现在是一个常量,但是不接受类型,因此也不起作用.在升级说明 中也找不到太多内容
编辑
尽管有趣的是,它可以编译:
return Observable.of<HttpEvent<any>>();解决方案
import {EMPTY} from 'rxjs';
用
替换返回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() with
return EMPTY;
更多推荐
RXJS 6:HttpInterceptor的新版本
发布评论