类型 'undefined' 不可分配给类型 'ObservableInput<any>

时间:2021-02-14 12:20:23

标签: angular typescript rxjs

这是我的通用拦截器,我在每个项目中都使用它 在我将 Angular 版本更新为 11 之后 我发现错误非常感谢您帮助我

<块引用>

输入'可观察的| undefined' 不可分配给类型 '可观察输入'。 类型 'undefined' 不能分配给类型 'ObservableInput'。

import { Injectable } from '@angular/core';
    import {
      HttpInterceptor,
      HttpRequest,
      HttpHandler,
      HttpEvent,
      HttpErrorResponse,
      HTTP_INTERCEPTORS,
    } from '@angular/common/http';
    import { Observable, throwError } from 'rxjs';
    import { catchError } from 'rxjs/operators'
    
    @Injectable()
    export class ErrorInterceptor implements HttpInterceptor {
      intercept(
        req: HttpRequest<any>,
        next: HttpHandler
      ): Observable<HttpEvent<any>> {
        return next.handle(req).pipe(
          catchError(error => {
              if (error instanceof HttpErrorResponse) {
              const applicationError = error.headers.get('Application-Error');
              if (applicationError) {
                console.error(applicationError);
                return throwError(applicationError);
              }
              const serverError = error.error;
              let modelStateErrors = '';
              if (serverError.errors && typeof serverError.errors === 'object') {
                for (const key in serverError.errors) {
                  if (serverError.errors[key]) {
                    modelStateErrors += serverError.errors[key] + '\n';
                  }
                }
              }
              if (error.status === 401) {
                return throwError(error.statusText);
              }
              return throwError(modelStateErrors || serverError || 'Server Error');
            }
          })
        );
      }
    }
    
    export \const ErrorInterceptorProvider = {
      provide: HTTP_INTERCEPTORS,
      useClass: ErrorInterceptor, 
      multi: true,
    };

0 个答案:

没有答案
相关问题