Angular 6-修改拦截器中的响应主体不起作用

时间:2018-08-24 09:40:13

标签: angular

我在使用HttpInterceptor修改响应正文时遇到问题。这是我的代码:

    return next.handle(modifiedReq).pipe(tap((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
            return event.clone({ body: this.modifyBody(event.body) });
        }
        return event;
    }, error => {
        this.modifyError(error);
        return of(error);
    }));

    private modifyBody(body: any) {
        //modify body here and return it
    }

正在调用Interceptor,但不幸的是,事件主体在被修改后仍然相同。在return event;,事件对象具有我的修改主体,但是当我跳到调试调试步骤时,我可以看到事件是源主体,并且我的服务也无效,因为预期的修改主体是源主体。

我只是试图通过更改如下主体来修改事件:

event.body = this.modifyBody(event.body);

这实际上是可行的,但是我在有角度的CLI中出现了错误,因为body-property是一个常数,并且在正式的uguglar教程中,它们总是像我的情况一样进行克隆。

那么有人有类似的问题吗,或者可以给我任何建议如何进行此操作?

1 个答案:

答案 0 :(得分:5)

只需尝试使用map而不是tap

https://www.learnrxjs.io/operators/utility/do.html 点击不会修改结果

相关问题