如何用预定义的管算机导出RXJS主题?
我想以一些预定义的行为导出RXJS主题(通过pipe()
)。
例如,我想在每个新值上导出一个执行console.log
的主题:
export const subj$ = new Subject()
.pipe(
tap(() => console.log('updated')
)
这有效,我可以subscribe()
subj $ < /代码>。
问题在于,这种方式是不可能做subj $ .next('foo')
。
当然,我可以将两个变量导出subscribe()
和next()
,但这是愚蠢的晚餐
I want to export rxjs Subject with some pre-defined behavior (via pipe()
).
e.g. I want to export a subject that do console.log
on each new value:
export const subj$ = new Subject()
.pipe(
tap(() => console.log('updated')
)
This works, and I'm able to subscribe()
to subj$
.
The problem is that this way it's impossible to do subj$.next('foo')
.
Of course I can export two variables for subscribe()
and for next()
, but that's supper silly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您不能这样做。因为
pipe
将主题
转换为可观察
,并且此记录与此相似:new objoce()。asobservable()
。但是,您可以创建自己的
主题
,例如从源( https://github.com/reactivex/rxjs/blob/master/src/src/internal/behaviorsubject.ts )。并使任何功能都在内部调用。Actually you cannot do this. Because
pipe
convertsSubject
toObservable
and this record is similar to this:new Subject().asObservable()
.But, you can create your own
Subject
, for example from source (https://github.com/ReactiveX/rxjs/blob/master/src/internal/BehaviorSubject.ts). And make any function to invoke inside.