简体   繁体   English

为什么在 RxJS 中管道 BehaviorSubject 会创建一个 AnonymousSubject?

[英]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

Examples:例子:

 // 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:让我们更深入地研究一下您的示例:

  1. You are instantiating a BehaviorSubject which extends Subject您正在实例化一个扩展SubjectBehaviorSubject
  2. You are calling pluck operator which internally calls map operator您正在调用内部调用map运算符的pluck运算符
  3. 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.

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