gpt4 book ai didi

RXJS 如何在嵌套订阅中优化 ForEach?

转载 作者:行者123 更新时间:2023-12-05 04:23:53 25 4
gpt4 key购买 nike

我对 observables 还比较陌生,仍在学习,我需要一些帮助。我有一个包含一些广告系列的 slider ,对于每个广告系列,我都有一个制作人,我需要获取该制作人并将其设置为广告系列。

我知道在这里使用管道、合并和 MergeMap 可能是一些解决方案,但我似乎无法让它工作,有人对我有想法吗?

这是我目前拥有的。

  fetchCampaigns() {
this.loading = true;
this.campaignService
.getCampaigns(
this.campaignAmount,
this.sorting,
this.desc
)
.subscribe(
campaignListDto => {
this.campaigns = campaignListDto;
this.campaigns.map(campaign => {
this.producerService.getProducerByName(campaign.producer_name)
.subscribe((producer) => {
campaign.producer = producer
campaign.producer_name
})
})
this.loading = false;
this.fetchProducerMinPrices();
this.fetchProducerTotalCampaigns();
})
};

最佳答案

尝试:

fetchCampaigns() {
this.loading = true;
this.campaignService.getCampaigns(this.campaignAmount,this.sorting,this.desc).pipe(
switchMap(campaignListDto => forkJoin(campaignListDto.map(
campaign => this.producerService.getProducerByName(campaign.producer_name).pipe(
map(producer => ({ ...campaign, producer })),
),
))),
).subscribe(campaignListDtoExtended => {
this.campaigns = campaignListDtoExtended;
this.loading = false;
this.fetchProducerMinPrices();
this.fetchProducerTotalCampaigns();
});
}

关于RXJS 如何在嵌套订阅中优化 ForEach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73608673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com