我在如何并行组合多个 http get 请求并将其作为平面、可观察数组返回的问题上遇到了问题。
目前,我有一个方法 returnNewCars()
,它在发出一个 http get 请求后返回 Observable<ICar[]>
-- 在方法 returnAllCars()
中,我想发出多个 http get 请求并且仍然返回 Observable<ICar[]>
。
现在,returnNewCars()
打印:
(2) [{…}, {…}]
0: {Make: "Honda", Model: "CRV", Year: "2021", Specifications: Array(5)}
1: {Make: "Toyota", Model: "Camry", Year: "2021", Specifications: Array(5)}
length: 2
我希望 returnAllCars()
以相同格式打印,但打印所有 6 个项目。
我遵循 RxJS doc regarding forkJoin 并尝试在我的代码中反映它,但是,我不知道从哪里开始。
app.component.ts
import { Component, OnInit } from '@angular/core';
import { CarsService } from './services/cars.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'AutoZone';
constructor(private carsService: CarsService){
}
ngOnInit(): void {
}
testConsole(){
this.carsService.returnNewCars().subscribe(newCars => console.log(newCars));
}
}
cars.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from "@angular/core";
import { forkJoin, Observable } from 'rxjs';
import { concatMap, map, tap } from 'rxjs/operators';
import { ICar } from '../models/cars.model';
@Injectable({
providedIn: 'root'
})
export class CarsService{
carsURL = '/assets/cars.mock.json';
newCarsURL = '/assets/cars.new.mock.json';
preownedCarsURL = '/assets/cars.preowned.mock.json';
usedCarsURL = '/assets/cars.used.mock.json';
private newCars$: Observable<ICar[]>;
//Store all http get request to new, preowned, used
private allCars$: Observable<ICar[]>;
constructor(private http : HttpClient){
}
returnNewCars(): Observable<ICar[]>{
this.newCars$ = this.http.get<ICar[]>(this.newCarsURL);
return this.newCars$;
}
returnAllCars(): Observable<ICar[]>{
//How do I flatten to return Observable<ICar[]>?
forkJoin(
{
new: this.http.get<ICar[]>(this.newCarsURL),
preowned: this.http.get<ICar[]>(this.preownedCarsURL),
used: this.http.get<ICar[]>(this.usedCarsURL)
}
)
return null;
}
}
答案 0 :(得分:1)
我不知道为什么你需要这个逻辑来使用一种方法获取所有汽车,因为你应该进行 api 调用来分别获取 3 种汽车列表。无论如何,我尝试了一种方法来获得 All 如您所料并使用 forkJoin。
答案 1 :(得分:1)
您只需添加一个 .pipe(map(...))
即可将 3 个单独的数组转换为一个数组。
我看不出有任何理由需要将对象传递给 forkJoin
,您只需传递一个数组即可:
returnAllCars(): Observable<ICar[]>{
return forkJoin([
this.http.get<ICar[]>(this.newCarsURL),
this.http.get<ICar[]>(this.preownedCarsURL),
this.http.get<ICar[]>(this.usedCarsURL)
]).pipe(
map(([new, preowned, used]) => [...new, ...preowned, ...used])
);
}
另外,Observables 是惰性的,所以没有必要将它们包装在这样的方法中:
returnNewCars(): Observable<ICar[]>{
this.newCars$ = this.http.get<ICar[]>(this.newCarsURL);
return this.newCars$;
}
你可以简单地定义它:
private newCars$ = this.http.get<ICar[]>(this.newCarsURL);
因此,对于 allCars()
,您可以简单地执行以下操作:
private allCars$ = forkJoin([this.newCars$, this.preOwnedCars$, this.usedCars$])
.pipe(
map(([new, preowned, used]) => [...new, ...preowned, ...used])
);