ForkJoin 2 BehaviorSubjects [英] ForkJoin 2 BehaviorSubjects
问题描述
我有两个行为主题流,我想在没有运气的情况下分叉加入.正如我想象的那样,它返回了它的最后两个值.这有可能以某种方式实现吗?
I have two behaviour subject streams what I'm trying to forkJoin with no luck. As I imagined it gives back the two last values of it. Is this possible to implement it somehow?
它不是在主语之后调用.
It is not called after the subject.
let stream1 = new BehaviorSubject(2);
let stream2 = new BehaviorSubject('two');
Observable.forkJoin(stream1, stream2)
.subscribe(r => {
console.log(r);
});
推荐答案
注意什么 forkJoin()
实际上是从它的文档中得到的:
Note what forkJoin()
actually does from its documentation:
等待 Observable 完成,然后合并它们最后发出的值.
Wait for Observables to complete and then combine last values they emitted.
这意味着 forkJoin()
在所有输入 Observable 完成时发出一个值.当使用 BehaviorSubject
时,这意味着在它们两个上显式调用 complete()
:
This means that forkJoin()
emits a value when all input Observable are complete. When using BehaviorSubject
this means explicitly calling complete()
on both of them:
import { Observable, BehaviorSubject, forkJoin } from 'rxjs';
const stream1 = new BehaviorSubject(2);
const stream2 = new BehaviorSubject('two');
forkJoin(stream1, stream2)
.subscribe(r => {
console.log(r);
});
stream1.complete();
stream2.complete();
观看现场演示:https://stackblitz.com/edit/rxjs-9nqtx6
2019 年 3 月:针对 RxJS 6 进行了更新.
March 2019: Updated for RxJS 6.
这篇关于ForkJoin 2 BehaviorSubjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!