我想修改此函式以在單獨的請求中發送這兩個檔案 ID:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body);
})
);
我應該使用平面地圖嗎?
uj5u.com熱心網友回復:
您可以像這樣從一個管道執行單獨的請求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return [
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
]
}),
mergeAll(),
);
uj5u.com熱心網友回復:
正確的運算子取決于您是要并行還是連續發送兩個請求。
如果你已經有了,take(1)那么你可以同時使用兩者,switchMap或者mergeMap因為它總是只發出一次,因此在這種情況下無關緊要。
并行發送請求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return forkJoin([
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
]);
}),
);
按順序發送請求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return concat(
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api EndPoint.CreateUser, body),
);
}),
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/339125.html
