首页 > 解决方案 > 如何在拦截中获得身体反应?

问题描述

我使用最后一个 Angular 并且需要在这里截获正文请求:

return next.handle(request).pipe(
            catchError(err => {
            return throwError(err);
        }))

我试图这样做:

return next.handle(request).pipe(
    .map((res: Response) => {
       if (res.result.hasOwnProperty('errors')) {
       }

      return res;
    })
catchError(err => {
   return throwError(err);
}));

但这对我不起作用

标签: angularangular7

解决方案


而不是map使用tap, 来获取响应并执行一些与之相关的操作,而tap您不需要返回 smth.

   return next.handle(request).pipe(
      tap(response => {
        if (response instanceof HttpResponse) {
          console.log(response);       
        }
      }, e => {
         console.log(e);
      })
   )

推荐阅读