在我的应用中,有时我具有以下代码:
this.missionHelperService.checkMissionPoint(position).subscribe(result => {
});
现在,checkMissionPoint应该执行以下=>
我试图使其返回可观察的结果,但我无法设法找到正确的语法
checkMissionPoint(position: Cartographic): Observable<CheckMissionResponse> {
const params = {
latitude: CesiumMath.rad2Deg(position.latitude),
longitude: CesiumMath.rad2Deg(position.longitude),
altitude: position.height,
};
return this.store$.pipe(select(TransactionsStoreSelectors.selectedTransactionId), take(1)).pipe(
map((tId) => {
return this.httpClient.post<any>(`${environment.API_URL}/trajectories/${tId}/checkpoints`, params);
})
)
}
我该怎么做?
答案 0 :(得分:3)
您可以尝试使用switchMap,因为您将返回一个新的observable:
checkMissionPoint(position: Cartographic): Observable<CheckMissionResponse> {
const params = {
latitude: CesiumMath.rad2Deg(position.latitude),
longitude: CesiumMath.rad2Deg(position.longitude),
altitude: position.height,
};
return this.store$.pipe(
select(TransactionsStoreSelectors.selectedTransactionId),
take(1),
switchMap((tId) => {
return this.httpClient.post<any>(`${environment.API_URL}/trajectories/${tId}/checkpoints`, params);
})
);
}