【问题标题】:How to use the result of an inner observable while using ngrx selector?使用ngrx选择器时如何使用内部可观察的结果?
【发布时间】:2019-10-23 08:11:27
【问题描述】:

在我的应用程序中,有时我有以下代码:

  this.missionHelperService.checkMissionPoint(position).subscribe(result => {

  });

现在,checkMissionPoint 应该执行以下操作 =>

  1. -从商店获取价值
  2. -执行 API 调用
  3. -返回API结果

我试图让它返回 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)).pipe(
      map((tId) => {
        return this.httpClient.post<any>(`${environment.API_URL}/trajectories/${tId}/checkpoints`, params);
      })
    )
  }

我该怎么做?

【问题讨论】:

  • 那么你需要做两次api调用吗?
  • 不,我需要一个 api 调用,然后订阅结果。

标签: angular typescript rxjs observable ngrx


【解决方案1】:

您可以尝试 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);
    })
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多