VueJS: viewing two properties

Suppose I have two data properties:

data() {
  return {
    a: false, 
    b: false, 
  }
}

      

When a

and b

become true

at the same time, I want to complete a related task.

How can I use the watch method in Vue to achieve this?

+3


source to share


1 answer


Keep track of the calculated value.

computed:{
    combined(){
        return this.a && this.b
    }
}
watch:{
    combined(value){
        if (value)
            //do something
    }
}

      



There is some short hand for the above using $ watch .

vm.$watch(
  function () {
    return this.a + this.b
  },
  function (newVal, oldVal) {
    // do something
  }
)

      

+6


source







All Articles