【问题标题】:How to send GET request sequentially using observables of observables如何使用可观察对象的可观察对象按顺序发送 GET 请求
【发布时间】:2021-05-03 20:31:40
【问题描述】:

我正在构建一个过滤器,用户可以通过单击离子芯片来过滤元素列表。每次点击都会使用 rxjs 触发对外部后端的调用。 应按顺序使用请求响应来更新元素列表。 我有一个数组(filterArray),其中包含点击的离子芯片的数据。 然后应该将其发送到后端。 我在这里的第一次尝试创建了所述数组的可观察对象(可观察对象)。 然后,我将此 observable 的元素映射到对我的服务的 getData 的调用,该调用触发 get 请求,返回带有后端响应的 observable。 然后应在过滤器对象中设置此数据。

如果我将数据推送到 filterArray,则不会发生任何事情。 谁能给我一个提示这里可能有什么问题? 提前谢谢!

filterArray: any[] = [];

ngOnInit() {
    const observables = from(this.filterArray);
    const combined = observables.pipe(
      map((filterData) => {
        return this.contentService.getData(filterData);
      }),
      concatAll()
    );
    combined.subscribe(data => {
      this.filter.filteredContent = data;
    });
  }

onChipSelected(val) {
  this.filterArray.push(val)
}

【问题讨论】:

  • 问题是否仍然存在?我的回答有用吗?

标签: angular ionic-framework rxjs


【解决方案1】:

你的observables observable 是冷的,你想要一个“热”流。 一种可能的方法是这样做:

filterArray: any[] = [];
filterArrayObs$: any[] = new BehaviorSubject(this.filterArray);

ngOnInit() {
    const combined = this.filterArrayObs$.pipe(
      map((filterData) => {
        return this.contentService.getData(filterData);
      }),
      concatAll()
    );
    combined.subscribe(data => {
      this.filter.filteredContent = data;
    });
  }

onChipSelected(val) {
  this.filterArray.push(val);
  this.filterArrayObs$.push(this.filterArray);
}

【讨论】:

    猜你喜欢
    • 2017-03-31
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多