如何用预定义的管算机导出RXJS主题?

发布于 2025-02-11 09:03:07 字数 427 浏览 1 评论 0原文

我想以一些预定义的行为导出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2025-02-18 09:03:07

实际上,您不能这样做。因为pipe主题转换为可观察,并且此记录与此相似:new objoce()。asobservable()

但是,您可以创建自己的主题,例如从源( https://github.com/reactivex/rxjs/blob/master/src/src/internal/behaviorsubject.ts )。并使任何功能都在内部调用。

Actually you cannot do this. Because pipe converts Subject to Observable 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文