[英]NgRedux + Angular + select triggered on subscribe
I'm using redux in my angular App for weeks now, but I just noticed something about it : 我已经在我的角度应用程序中使用redux数周了,但是我刚刚注意到了一些事情:
When I'm selecting a part of my store to monitor changes, let's say like that : 当我选择商店的一部分来监视更改时,可以这样说:
In the ngOnInit() of app.component.ts 在app.component.ts的ngOnInit()中
this.ngRedux.select(s => s.counter).subscribe((counter: number) => {
console.log(counter)
});
The console.log(counter) is executed each time my counter value change, that's ok, that's how it suppose to works. 每当我的计数器值更改时,都会执行console.log(counter),没关系,这就是它应该起作用的方式。
BUT, the callback is also executed when I first subscribe. 但是,当我第一次订阅时,也会执行回调。 So it's like that :
就像这样:
My problem is that I dont want my callback to be executed on step 4, because the store did not change ! 我的问题是我不希望在步骤4上执行回调,因为存储没有更改!
Is there an issue in my understanding ? 我的理解有问题吗? Is is supposed to work like that ?
应该像那样工作吗? If yes : Can I do something to change it ?
如果是,我可以做些改变吗?
Thanks 谢谢
Unfortunately, this is the standard behaviour of .subscribe()
. 不幸的是,这是
.subscribe()
的标准行为。 The only thing you can do is to compare your current value of counter
with the new one and only react on it if changes happened. 您唯一可以做的就是将
counter
的当前值与新counter
进行比较,并且仅在发生更改时对此做出反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.