如何根据多个数据订阅返回我自己的可观察量



挣扎了几个小时......如何返回依赖于其他可观察量的多个链式订阅的自定义可观察量。比如,我调用一个函数并订阅并检查可观察量的值,然后决定调用哪个其他函数。然后调用另一个 http 请求以从该请求中获取新的可观察量。最后,根据以前的可观察量返回我的自定义可观察量。

https://medium.com/@danielt1263/recipes-for-combining-observables-in-rxswift-ec4f8157265f 有多种

方法可以做到这一点

this.homeworld = this.http.get('/api/people/1').pipe(
 mergeMap(character => this.http.get(character.homeworld)));
这是一个可观察

量如何依赖于另一个可观察量的简单示例,对您的问题给出确切答案仍然是一个问题,因此您可以在 mergeMap 中搜索。

使用 switchMap 运算符

import {switchMap} from "rxjs/operators";
import of as off from "rxjs";
      processData().pipe(switchMap(result)=>{
       if(result=="first"){
         return this.httpService.get("/api/first");
       }
       return this.httpService.get("/api/second");
    }).pipe(switchMap(response)=>{
       if(response==true){
       return off("success");  
    }
     return off("fail");
    })

最新更新