如何运行可观察对象取决于Angular中RxJS的先前可观察对象的结果?

问题描述 投票:0回答:2

我需要做这些事情:

  • 从远程服务器删除项目
  • 如果成功,则从本地存储中删除项目
  • 如果成功,则从远程服务器重新加载项目

现在我有了这段代码,但是它不能正常运行,因为第三步没有运行。

delete(model: T, paginate: PaginateInterface): Observable<any> {
  return this.remoteStorageService.delete(model).pipe(map((resultDelete: boolean) => {
    if (resultDelete) {
      this.localStorageService.delete(model);
      return this.remoteStorageService.getAll().pipe(map( (resultGetAll: PaginateInterface) => {
        return resultGetAll;
      }));
    }

    return paginate;
  }));
}

我试图使用RxJS switchMap函数,但我完全迷路了。

我如何运行一个函数取决于可观察对象的结果,然后另一个可观察对象返回的结果是什么?

angular rxjs rxjs6
2个回答
0
投票

我假设步骤1和3是组件的工作,而步骤2是服务的工作。


0
投票

从您发布的代码和注释中,我假设remoteStorageService.deleteremoteStorageService.getAll()返回一些可观察值,而localStorageService.delete()返回void

© www.soinside.com 2019 - 2024. All rights reserved.