0

Problem

Changes to an object are not being detected by the listening child component.

Context

I have an empty object that is storing values as they come back from an API. This object is bound to a property in a child component.

data () {
  return {
    filterOperators: {}
  };
},

Each time this method is called, a named array containing the response is added to the object.

getfilterOperators: function (fieldName) {
  this.filterOperatorsAction(fieldName, response => {
    this.$data.filterOperators[fieldName] = response.Data;
  });
}
1
  • What's the question? Can you provide a jsFiddle that highlights the problem? Commented Nov 2, 2016 at 20:33

2 Answers 2

1

In VueJS, properties added to objects are not reactive. You need to use the vm.$set method to make them reactive:

getfilterOperators: function (fieldName) {
  this.filterOperatorsAction(fieldName, response => {
    this.$set(this.filterOperators,fieldName,response.data);
  });
}

You can read more information on this page: Reactivity in Depth

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

1 Comment

Hm. It fails with a message of TypeError: exp.trim is not a function. (In 'exp.trim()', 'exp.trim' is undefined). I also tried this.$set(this.$data.filterOperators, sFieldName, response.Data); with no success.
0

For Vue 1, it should be constructed like this in order to allow Vue to detect the change to the object:

this.$set('filterOperators.' + fieldName, response.data);

Reference: Reactivity in Depth

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.