RXJS可观察的发射顺序
例如,我如何订阅三个可观察量,并在其中一个发出新值时发出取决于它们的顺序,例如 forkJoin 但发出,但它们的顺序很重要,所以如果 ob3 首先发出,我希望 obv1 的值为 null,也为 obv2 并且只有发出的 obv3 才有价值,
例如
forkJoin(obs1,obs2,ob3)
.subscribe([ob1v,ob2v,ob3v]=>{
if (obv1v){ 'do somthing'}
if (obv2v){ 'do somthing'}
if (obv3v){ 'do somthing'}
})
谢谢
how can i subscribe for example three observables, and to emit when one of them emit new value depend on the order they are, like forkJoin but emit but the order of them is important so if ob3 emit first, i want the value of obv1 to be null, also obv2
and only obv3 that was emitted will have value
for example
forkJoin(obs1,obs2,ob3)
.subscribe([ob1v,ob2v,ob3v]=>{
if (obv1v){ 'do somthing'}
if (obv2v){ 'do somthing'}
if (obv3v){ 'do somthing'}
})
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许带有初始值的
combineLatest
适合您。每当任何源发出时,combineLatest
都会发出每个可观察源的最新值。但是,在所有源都发出至少一个值之前,它不会第一次发出。我们可以使用
startWith
通过提供初始值来克服这个问题每个来源。您可以在此 StackBlitz 中查看输出。
Maybe
combineLatest
with an initial value would work for you.combineLatest
will emit the latest value from each source observable whenever any of its sources emit. However, it doesn't emit for the first time until all sources have emitted at least one value.We can use
startWith
to overcome this by providing an initial value for each source.You can see the output in this StackBlitz.
看来你想对每个可观察到的东西做不同的事情。也许你不应该把它们分组?如果你想对它们进行分组并对每个它们执行不同的副作用,你可以执行类似于 BizzyBob anwer 的操作,但不要在订阅中使用 if 语句,而是使用 tap() 运算符。像这样的事情:
好的做法是不要使用订阅方法,而是将此流设置为组件中的某个属性,然后在模板中使用异步管道。
It seems that you want to do different thing for every observable. Maybe you shouldn't gorup them? If you want to group them and do different side effect for every one of them you can do something similar to BizzyBob anwer but instead of having if statements in subscribe use tap() operator for every stream. Something like this:
Good practise is not to use subscribe method but instead set this stream to some property in component and than use async pipe in the template.