简体   繁体   English

如何使用预定义的管道运算符导出 rxjs 主题?

[英]How to export rxjs Subject with pre-defined pipe operators?

I want to export rxjs Subject with some pre-defined behavior (via pipe() ).我想用一些预定义的行为(通过pipe() )导出 rxjs 主题。

eg I want to export a subject that do console.log on each new value:例如,我想导出一个对每个新值执行console.log的主题:

export const subj$ = new Subject()
  .pipe(
    tap(() => console.log('updated')
  )

This works, and I'm able to subscribe() to subj$ .这行得通,我可以subscribe()subj$

The problem is that this way it's impossible to do subj$.next('foo') .问题是这样就不可能做到subj$.next('foo')

Of course I can export two variables for subscribe() and for next() , but that's supper silly当然,我可以为subscribe()next()导出两个变量,但这太愚蠢了

Actually you cannot do this.实际上你不能这样做。 Because pipe converts Subject to Observable and this record is similar to this: new Subject().asObservable() .因为pipeSubject转换为Observable并且此记录类似于: 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 ).但是,您可以创建自己的Subject ,例如从源代码 ( https://github.com/ReactiveX/rxjs/blob/master/src/internal/BehaviorSubject.ts )。 And make any function to invoke inside.并在内部调用任何函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM