API 回傳一組 JSON 物件。
[
{
"vacancy": "61a6597b0dc105d6e79a1f30",
"createdBy": "61aa11644afa183fa28b0792",
"executor": "61aa20ee25ef06b69920a505",
"reviewer": "61aa11644afa183fa28b0792",
"status": "Invited",
"_id": "61aa213aeaf2af804aa1e591",
"createdAt": "2021-12-03T13:52:58.772Z",
"updatedAt": "2021-12-03T13:52:58.772Z"
},
...
]
我需要通過使用值進行 get 請求將一些屬性轉換為物件。類似的東西:
this.http.get<Application>(
`${environment.API_ENDPOINT}/applications/assigned?status=completed`
).pipe(
map(aplication =>
{
...aplication,
vacancy:this.http.get(`${environment.API_ENDPOINT}/vacancys/` aplication.vacancy),
executor:this.http.get(`${environment.API_ENDPOINT}/candidates/` aplication.executor)
}
)
uj5u.com熱心網友回復:
您需要使用“高階映射運算子”,它將在內部訂閱可觀察物件并發出其排放。
在你的情況下,switchMap將為此作業。由于您需要進行兩次不同的呼叫,因此我們可以使用forkJoin來創建一個 observable,該 observable 將在收到結果時發出兩個結果:
myObj$ = this.http.get<Application>('/applications/assigned').pipe(
switchMap(aplication => forkJoin({
vacancy : this.http.get(`environment.API_ENDPOINT}/vacancys/${vacancy}`),
executor : this.http.get(`environment.API_ENDPOINT}/candidates/${executor}`)
}).pipe(
map(({vacancy, executor}) => ({...aplication, vacancy, executor})
))
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/373502.html
