[英]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()
.因为
pipe
将Subject
转换为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.