gpt4 book ai didi

javascript - Angular 组合来自不同方法的数据

转载 作者:行者123 更新时间:2023-11-29 20:26:55 24 4
gpt4 key购买 nike

我有来自三个不同服务的数据,我想将来自所有服务的数据合并到一个方法中

this.data1 = this.activity1Service.getAllMonth(); 
this.data2 = this.activity2Service.getAllMonth();
this.data3 = this.activity3Service.getAllMonth();

this.alldata = concat (this.data1,this.data2,this.data3);

并作为 Observable 调用

GetAllData(): Observable<Data[]>{


return (this.alldata);

}

然后也许做 ngOnInit()


ngOnInit() {

this.dataService.getAllData().subscribe(aldata => this.AllData = aldata);
}

我不确定如何将来自不同服务的数据组合成一种方法,请任何人帮忙。

最佳答案

您可以使用forkJoin

forkJoin waits for each HTTP request to complete and group’s all the observables returned by each HTTP call into a single observable array and finally return that observable array.

类文件

getAllData() {
this.data1 = this.activity1Service.getAllMonth();
this.data2 = this.activity2Service.getAllMonth();
this.data3 = this.activity3Service.getAllMonth();
return forkJoin([this.data1, this.data2, this.data3]);
}


ngOnInit() {

this.getAllData().subscribe(aldata => {
console.log(data);
this.AllData = aldata
});
}

关于javascript - Angular 组合来自不同方法的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59138833/

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