1

I want to watch two variables (or more) and trigger a method only if both variables change. So far, I have only figured out how to watch multiple elements and trigger a function when one element changes.

is there any solution ?

4
  • have 2 single watchers and have a check inside each to see if the other value has also changed Commented Mar 25, 2020 at 12:33
  • Why don't you check if the two variables are set in the function that should be called from the two watchers? Commented Mar 25, 2020 at 12:34
  • the variables are in the store, and the watcher and the method are in a mixin. @depperm how do you check in a watcher if the other value changed ? Commented Mar 25, 2020 at 12:52
  • @MajedBadawi I need to check if the variabled have been changed and not to check if they are set Commented Mar 25, 2020 at 12:55

1 Answer 1

2

You can try the following:

      computed: {
        twoVariables: {
          return [this.var1, this.var2]
        }
      },
      watch: {
        twoVariables(newValue, oldValue) {
          if (newValue[0] !== oldValue[0] && newValue[1] !== oldValue[1]) {
            //do stuff
          }
        }
      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.