Error: Cannot read properties of undefined (reading 'pipe') Angular12

Error: Cannot read properties of undefined (reading 'pipe') Angular12

发送 ajax 请求时出现以下错误,

ERROR TypeError: Cannot read properties of undefined (reading 'pipe')

ajax-loader.interceptor.ts class

 export class AjaxLoaderInterceptor implements HttpInterceptor {
 private loaderProp = 'ajax_loader';
 constructor(private activeAjaxLoadersService: ActiveAjaxLoadersService) {}

 intercept(
   request: HttpRequest<any>,
   httpHandler: HttpHandler
 ): Observable<HttpEvent<any>> {
   console.log('ajax loader', request);
   const loaderName = request.headers.has(this.loaderProp)
     ? request.headers.get(this.loaderProp)
     : null;
   console.log('loaderName', loaderName);
   const updatedHeaders = request.headers.has(this.loaderProp)
     ? request.headers.delete(this.loaderProp)
     : request.headers;
   const updatedRequest = request.clone({ headers: updatedHeaders });
   console.log('updatedHeaders', updatedHeaders, updatedRequest);
   this.updateLoaderState(loaderName, XHR_REQUEST_STATES.PROGRESS);

   return httpHandler.handle(updatedRequest).pipe(
     tap((event: HttpEvent<any>) => {
       console.log('httpHandler', event);
       if (event instanceof HttpResponse) {
         this.updateLoaderState(loaderName, 'success', event);
       }
     }),
     (responseError: any): any => {
       if (responseError instanceof HttpErrorResponse) {
       }
     }
   );
 }

Providers code:

providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AjaxLoaderInterceptor ,
      multi: true
    }
  ]

相同的代码在 angular 8 中工作,但在升级到 v12 后它坏了。

我已经在 stackblitz 中创建了可重现的代码示例,请在此处找到它 Example

如有任何帮助,我们将不胜感激。 谢谢!

在您的 ajax-loader.interceptor.ts 文件中,在 if 条件之后添加 return 语句,如下所示

   if (responseError instanceof HttpErrorResponse) {
       }

    return responseError;