本文介绍了管道 RxJS 可观察到现有主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
存在正在使用的主题:
const fooSubject = new BehaviorSubject(null);还有另一个 observable(本例中的另一个主题):
And there is another observable (another subject in this example):
const barSubject = new Subject(); barSubject.subscribe( value => fooSubject.next(), err => fooSubject.error(err), () => fooSubjectplete() ); barSubject.next('bar');代码有效,但看起来很笨拙.
The code works but looks clumsy.
是否有更好的管道方法(广义上,不一定使用 pipe 运算符)barSubject 可观察到 fooSubject?看起来像是库本身可以处理的操作.
Is there a better way to pipe (in broad sense, not necessarily using pipe operator) barSubject observable to fooSubject? It looks like an operation that could be handled by the library itself.
推荐答案因为 Subject 已经是一个观察者,有方法 next(), error() 和 complete() 你可以订阅任何 Observable :
Since Subject is already an observer with methods next(), error() and complete() you can just subscribe it to any Observable:
const fooSubject = new BehaviorSubject(null); const barSubject = new Subject(); barSubject.subscribe(fooSubject); barSubject.next('bar');更多推荐
管道 RxJS 可观察到现有主题
发布评论