RXJS 6:HttpInterceptor的新版本 [英] RXJS 6: new version of HttpInterceptor

查看:77
本文介绍了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>>();

解决方案

  1. import {EMPTY} from 'rxjs';

  2. 替换返回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>>();

解决方案

  1. import {EMPTY} from 'rxjs';

  2. Replace return Observable.empty() with

    return EMPTY;

这篇关于RXJS 6:HttpInterceptor的新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆