我如何增加a<数字>重录的价值?
我正在使用chativioursubject,但我发现很难增加或减少
我要么创建一个无限循环的数字:
this.observer.pointsCollected$.subscribe(val => this.observer.pointsCollected$.next(val + 10));
或者我只使用一个更新,使用:
this.observer.pointsCollected$.pipe(take(1)).subscribe(val => this.observer.pointsCollected$.next(val + 10));
使用:
public pointsCollected$ = new ReplaySubject<number>();
我想 我已经使用actavionursubject并使用actabioUrsubject.getValue()解决了这个问题,
请告诉我是否这是一种好方法
I'm using a BehaviourSubject but i'm finding it difficult to increase or decrease that number
I'm either creating an infinite loop using:
this.observer.pointsCollected$.subscribe(val => this.observer.pointsCollected$.next(val + 10));
Or i'm only getting a single update using:
this.observer.pointsCollected$.pipe(take(1)).subscribe(val => this.observer.pointsCollected$.next(val + 10));
I'm creating the observable using:
public pointsCollected$ = new ReplaySubject<number>();
I think I have solved this using a BehaviourSubject instead and using BehaviourSubject.getValue()
Please let me know if this is a good approach or not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
清楚定义所需的行为很重要。一个replaySubject(从可观察的意义上中)没有值,它只是一个向其订户散发值的对象。
说“ 增加一个replayubject 的值”还不够精确。
也许您想“ 发射先前发射的值增加了特定的数量”
这听起来像是
要设置该设置,我们可以拥有一个发出要添加的“增量”的主题,并得出一个可观察到的发出计算值的可观察值:
我们现在可以订阅
number $
并最初接收默认值。任何时候增量$ .next()
被调用,号码$
将发出上一个发行和新增量的总和。在Angular中,您可以将其包裹在服务中,并提供公开方法来增加数字:
这是一个工作 stackblitz 演示。
It's important to clearly define the desired behavior. A ReplaySubject (in the observable sense) doesn't have a value, it's merely an object that emits values to its subscribers.
To say "increase a value of a ReplaySubject" isn't quite precise enough.
Maybe you want to "emit the previously emitted value increased by a specific amount"
This sounds like a perfect use case for the
scan
operator as it will emit values based on the currently received value and the prior emitted value.To set the up we can have a Subject that emits the "increment" to be added and derive an observable that emits the calculated value:
We can now subscribe to
number$
and initially receive the default value. Any timeincrement$.next()
is called,number$
will emit the sum of the previous emission and the new increment.In Angular, you could wrap this into a service and provide a public method to increase the number:
Here's a working StackBlitz demo.
第一种方法是错误的,因为正如您所说,这是一个无限的循环。
我认为第二个是“增加重型对象的值”的最佳方法:它仅获得1个单个值(当前),然后仅将其递增一次。
如果您想有一个触发的“信号”,您可以将其包裹在一个主题周围:
但是我认为,最好反应地宣布可观察到可观察到,请避免尽可能多地挑战 - 首先是有点挑战 - 而不是思考“发生这种情况时,我需要将点收费的$提高到10”的命令式方式,您需要思考相反的方式”点收费$的价值是发生了多少次乘以10英寸。此示例将变成类似的内容:
如果您有多个订户并且想共享相同的值,则可以使用
ShareReplay(1)
保留最新值 - 类似于ReplaySubject,但无需<代码> .next 它。理想情况下,您也不会将
performincrement $
作为主题,这将是另一个可观察到的,只要您应该根据您的逻辑发生这种情况。The first approach is wrong because as you said, that's an infinite loop.
The second one I think its the best way "to increase the value of a ReplaySubject": it's getting just 1 single value (the current), and then incrementing it just once.
If you want to have a "signal" that triggers that, you could just wrap it around a Subject:
But in my opinion it's better to declare observables reactively, avoid ReplaySubjects/BehaviourSubjects as much as posible - it's a bit challenging first because instead of thinking the imperative way of "when this happens I need to increase pointsCollected$ by 10" you need to think the other way around "the value of pointsCollected$ is how many times this has happened multiplied by 10". This example would become something like:
If you have multiple subscribers and want to share the same value, you can use a
shareReplay(1)
to keep the latest value - Similar to a ReplaySubject but without having to.next
on it.And ideally you wouldn't have
performIncrement$
as a Subject either, it would be another observable that emits whenever that should happen based on your logic.我只是这样做了:
I just did this: