首页 > 解决方案 > Angular 4:一个订阅,两个 Http 调用

问题描述

我正在尝试以一种非常标准的方式将数据发布到服务器,我之前可能已经做过一百万次了,但是在这种特殊情况下,正在进行两个 http 调用。谁能帮助解释为什么会发生这种情况?

我将 Angular 4.2.4 与 Rxjs 5.4.3 一起使用,并使用HttpClient.

下面是相关的组件代码,使用 UI 中的事件处理程序调用。

this.visitsService
    .addMeasuredWeight(this.visit.Id, this.kid.Id, weight)
    .subscribe(result =>
      this.kid.Weights.push(MeasuredWeightDto.ToMeasuredWeight(result))
    );

这是服务代码

    public addMeasuredWeight(
          visitId: string,
          personId: string,
          measuredWeight: MeasuredWeight
    ): Observable<MeasuredWeightDto> {
      let url: string = `${this.getBaseUrl()}/${visitId}/persons/${personId}/weights`;

      return this.http.post<MeasuredWeightDto>(
        url,
        MeasuredWeightDto.FromMeasuredWeight(measuredWeight)
      );
    }

我已经通过调试器确认这些方法只被调用一次,但是在 Chrome DevTools 网络选项卡中,创建了两个 http 帖子,并且订阅结果处理程序确实运行了两次。下面是来自 DevTools 的屏幕截图,显示了多个帖子。

在此处输入图像描述

任何帮助确定为什么在此处进行多次调用的任何帮助都将不胜感激。如果需要任何其他信息,请告诉我。提前致谢。

标签: angularrxjs

解决方案


推荐阅读