问题描述
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
仅在所有可观察物都完成后才能起作用.
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()
来完成每个可观察到的源:
Use
take(1)
withforkJoin()
to complete each source Observable:
forkJoin(o1$.take(1), o2$.take(1))
使用 zip()
和take(1)
仅在所有源可观察对象发出相同数量的项目时发出:
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
这篇关于对于未完成的可观察变量的forkJoin替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!