Rxjs 6变平可观察

时间:2018-08-14 12:47:13

标签: angular rxjs

我需要您关于如何将Rxjs 5转换为Rxjs 6的建议。

我的代码在下面,我不太确定为什么它不起作用。

Rxjs 5

Observable.from([{amount:5},{amount:10}])
.flatMap(_ => depositService.deposit(depositAmount))
.toArray()
.subscribe(result => {
    console.log(result.length);
})

Rxjs 6

import { Observable, from, } from 'rxjs';
import { map, catchError, mergeMap} from 'rxjs/operators';
...

const source = from([{amount:5},{amount:10}]);
source
.pipe(mergeMap(_ => depositService.deposit(_.amount).toArray())
.subscribe(result => {
    console.log(result.length);
})

我遇到了错误

您提供了一个无效对象,该对象应在预期流的位置提供。您可以提供Observable,Promise,Array或Iterable。

1 个答案:

答案 0 :(得分:5)

我认为toArray是一个运算符,应该在管道中传递。

import { Observable, from, } from 'rxjs';
import { map, catchError, mergeMap, toArray} from 'rxjs/operators';
...

const source = from([{amount:5},{amount:10}]);
source
.pipe(
    mergeMap(_ => depositService.deposit(_.amount)),
    toArray()
)
.subscribe(result => {
    console.log(result.length);
})
相关问题