未完成的 observable 的 forkJoin 替代方案? [英] A forkJoin alternative for uncompleted observables?
问题描述
constructor(
private route: ActivatedRoute,
private http: Http
){
// Observe parameter changes
let paramObs = route.paramMap;
// Fetch data once
let dataObs = http.get('...');
// Subscribe to both observables,
// use both resolved values at the same level
}
是否有类似于 forkJoin
的东西在发出参数更改时触发?forkJoin
仅在所有 observable 完成后才有效.
Is there something similar to forkJoin
that triggers whenever a parameter change is emitted? forkJoin
only works when all observables have completed.
我只需要避免回调地狱,欢迎任何符合要求的替代方案.
I just need to avoid callback hell, any alternative that complies is welcome.
推荐答案
有几个选项:
使用
take(1)
和forkJoin()
来完成每个源 Observable:
Use
take(1)
withforkJoin()
to complete each source Observable:
forkJoin(o1$.take(1), o2$.take(1))
使用zip()
和 take(1)
仅在所有源 Observable 发出相同数量的项目时才发出:
Use zip()
and take(1)
to emit only when all source Observables have emitted the same number of items:
zip(o1$, o2$).take(1)
使用combineLatest()
在任何源 Observable 发出时发出:
Use combineLatest()
to emit when any of the source Observables emit:
combineLatest(o1$, o2$)
2019 年 1 月:针对 RxJS 6 更新
Jan 2019: Updated for RxJS 6
这篇关于未完成的 observable 的 forkJoin 替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!