在ngrx中订阅存储时,如何访问以前的状态和当前状态并进行比较? [英] How to access previous state and current state and compare them when you subscribe to store in ngrx?

查看:80
本文介绍了在ngrx中订阅存储时,如何访问以前的状态和当前状态并进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的组件中,我订阅了ngrx存储,该存储在给定状态更改时触发.我想设置一个条件,如果当前状态和先前状态不同,那么我将执行一些操作.

In my component I subscribe to ngrx store which is triggered on change in the given state. I want to set the condition that if current and previous states are different then I do some action.

如何获取以前的状态?

    this.store
      .select('testPortfolio')
      .subscribe(testPortfolio => {
        // if(testPortfolio.oldValue !== testPortfolio.currentValueValue) ??? ///something like this
        console.log(testPortfolio);
       });

推荐答案

您可以使用对此进行了明确的更改.在您的情况下,它将类似于:

You can use distinctuntilchanged for that. In your case it will be something like:

this.store
  .pipe(
    select('testPortfolio'),
    distinctUntilChanged((prev, curr) => prev.value === curr.value)
  )
  .subscribe(testPortfolio => {
    console.log(testPortfolio); // called only when testPortfolio value has been changed
  });

这篇关于在ngrx中订阅存储时,如何访问以前的状态和当前状态并进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆