0

If have a situation where I want to setup my 'reactivity' programatically, at run time.

I want a function like: dependsOn(dataValName, someOtherDataVal) where if someOtherDataVal changes, then dataValName is updated. e.g. dependsOn('a', 'b') if the value of b changes, then a is re-displayed/updated.

Does such a function exist?

2 Answers 2

2

Your question is vague, but what you probably need is a computed property. Vue will automatically detect the dependencies of computed properties. For example, there is no need to be explicit here:

new Vue({
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe',
    }
  },
  computed: {
    fullName() {
      return `${ firstName } ${ lastName }`;
    }
  }
})

Any time firstName or lastName changes, the computed property fullName will also be re-evaluated.

Sign up to request clarification or add additional context in comments.

2 Comments

Computed property will not work for me as I do not know ahead of time what fields to use. i.e. data is loaded from a database that determines what is dependent on what, hence the need for a "dependsOn" type of method. I have noticed there is an updated() method which is called every time a field changes - I may have to roll my own logic to do this and place it there.
Yep, sounds like something you'll have to implement yourself.
0

Sounds like you're talking about watchers. For example

data: () => ({
  dataValName: 'a',
  someOtherDataVal: 'b'
}),
watch: {
  someOtherDataVal (newValue) {
    this.dataValName = 'c' // or whatever
  }
}

Keep in mind, computed properties are almost always a better option.

4 Comments

Thanks Phil, but watcher no good for me for reason listed above.
"Listed above" where? Your question mentions nothing of this
See my reply to vince above.
Please add your requirements to the question

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.