C
C
camradee2022-01-10 18:14:18
Vue.js
camradee, 2022-01-10 18:14:18

How to track the state of two computed properties using logical AND in vue watch?

That is, there is a property A and B, it is necessary for watch to work when both A and B change. If there is no such built-in capability, then how could it be bypassed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
camradee, 2022-01-12
@camradee

In the end, how did. Added 2 state variables and merged 2 properties into one compoundAB:

data () {
    return {
      stateAChanged: false,
      stateBChanged: false
    }
  },
  computed: {
    getA () {
     ....
    },
    getB () {
     ....
    },
    compoundAB() {
      return `${this.getA}|${JSON.stringify(this.getB)}`
    }
  }

Property B is implied as an object. Watch checks state variables and if true is executed
watch: {
    compoundAB: function (newVal, oldVal) {
      const [oldA , oldB ] = oldVal.split('|');
      const [newA, newB] = newVal.split('|');

      if (oldA !== newA) {
        this.stateAChanged = true
      }
      if (oldB !== newB) {
        this.stateBChanged = true
      }
      if (this.stateAChanged && this.stateBChanged) {
        this.stateAChanged = this.stateBChanged = false
      }
    }
  },

A
Alex, 2022-01-10
@Kozack Vue.js

Inside watch, you are passed the new and old values ​​of your properties. Just check that both new values ​​are not equal to the old ones

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question