Angular 2将三个http调用与flatMap相结合? RxJs?
发布时间:2020-12-17 18:01:09 所属栏目:安全 来源:网络整理
导读:我可以找到很多链接两个呼叫的例子,但我有3个http呼叫使用前一个呼叫的数据一个接一个地进行. 我有两个使用flatMap工作 所以: call1(params) .flatMap((res1)= { return call2(params) .subscribe(r=r...) 但对于三个电话我正在尝试同样的事情,但我认为你不
我可以找到很多链接两个呼叫的例子,但我有3个http呼叫使用前一个呼叫的数据一个接一个地进行.
我有两个使用flatMap工作 所以: call1(params) .flatMap((res1)=> { return call2(params) .subscribe(r=>r...) 但对于三个电话我正在尝试同样的事情,但我认为你不能将flatMaps连在一起? call1(params) .flatMap((res1)=> { return call2(params) .flatMap((res2)=> { return call3(params) .subscribe(r=>r...) 我收到一个错误,说订阅不能分配给类型观察输入.每个call1都从http操作返回一个observable. 谁能指出我正确的方向? 真的很感激它,因为它让我疯了! 谢谢 解决方法
您可以根据需要多次使用flatMap.但是,每次都必须返回一个Observable.
例如. myFunc() // returns an Observable of type X .flatMap((res: X) => { // returns an Observable of type Y }) .flatMap((res: Y) => { // returns an Observable of type Z }) .flatMap((res: Z) => { // returns an Observable of type Q }) .subscribe((res: Q) => { // some logic }); RxJs发生了变化 从RxJs v5.5开始,出现了Pipeable操作符.我们不再将一些运算符原型化为Observables,而是导入它们并按如下方式使用它们: import { flatMap } from 'rxjs/operators'; myFunc() // returns an Observable of type X .pipe( flatMap((res: X) => { // returns an Observable of type Y }),flatMap((res: Y) => { // returns an Observable of type Z }),flatMap((res: Z) => { // returns an Observable of type Q }) ).subscribe((res: Q) => { // some logic }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |