管道 RxJS 可观察到现有主题

编程入门 行业动态 更新时间:2024-10-17 17:21:04
本文介绍了管道 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 可观察到现有主题

本文发布于:2023-11-27 23:28:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   主题   观察到   RxJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!