简体   繁体   English

NgRedux + Angular +选择在订阅时触发

[英]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 : 就像这样:

  1. Enter onNgInit() 输入onNgInit()
  2. Executing select() 执行select()
  3. Executing Subscribe() 执行Subscribe()
  4. Executing my callback 执行我的回调
  5. ------- Later when counter change : ------- -------以后换计数器时:-------
  6. Executing my callback 执行我的回调

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.

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