RXJS 6:HttpInterceptor的新版本

编程入门 行业动态 更新时间:2024-10-26 20:21:59
本文介绍了RXJS 6:HttpInterceptor的新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将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的新版本

    本文发布于:2023-11-24 19:21:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1626495.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:新版本   RXJS   HttpInterceptor

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!