[英]Why does piping a BehaviorSubject create an AnonymousSubject in RxJS?
When creating an RxJS BehaviorSubject
, it stays a BehaviorSubject
until it's pipe
'd.当创建一个 RxJS BehaviorSubject
,它一直是一个BehaviorSubject
直到它被pipe
'd。 As soon a pipe
'd version is returned, it becomes an AnonymousSubject
.一旦返回pipe
版本,它就会成为AnonymousSubject
。
// Instance of `BehaviorSubject` const behaviorSubject$ = new BehaviorSubject({ someValue: null }) // Suddenly becomes an Anonymous Subject const anonymousSubject$ = ( behaviorSubject$ .pipe( pluck('someValue') ) ) // Also suddenly becomes an Anonymous Subject const anonymousSubject$ = ( new BehaviorSubject({ someValue: null }) .pipe( pluck('someValue') ) )
I experience this same issue with ReplaySubject
as well.我在ReplaySubject
中也遇到了同样的问题。 I can't seem to pipe through the subject and return that subject back.我似乎无法通过这个主题并将该主题返回。 It always converts to an AnonymousSubject
.它总是转换为AnonymousSubject
。 I think what I'm looking for here is Promise-like behavior where I can subscribe to this observable from anywhere and grab the one value passed into it.我想我在这里寻找的是类似 Promise 的行为,我可以从任何地方订阅这个 observable 并获取传递给它的一个值。
This is happening due to lift
called on Subject
.发生这种情况是由于lift
调用了Subject
。
Let's take a deeper look at your example:让我们更深入地研究一下您的示例:
BehaviorSubject
which extends Subject
您正在实例化一个扩展Subject
的BehaviorSubject
pluck
operator which internally calls map
operator您正在调用内部调用map
运算符的pluck
运算符map
operator internally calls lift
on BehaviorSubject
which is delegated to Subject
which then returns an AnonymousSubject
map
操作符在内部调用BehaviorSubject
上的lift
,它被委托给Subject
,然后返回一个AnonymousSubject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.