0

Although this is done in vuejs, I strongly believe this is plain javascript related, I have this doc_types array, when I try to assign some value to its index it isn't working, but when I try to push it works fine..

data: function () {
    return {
      doc_types: [],
    }
  },
  methods:  {
      populateDocType() {

                 this.doc_types.push(response.data); //this works
                 this.doc_types[0] = response.data //this doesn't...no errors though..just the array is unmodified..

      }
  }
2
  • Can you log this.doc_types[0] before and after trying to assign data? Commented Sep 3, 2018 at 12:11
  • the first statement adds to the array a new value, while the second assigns to the first value of the array the result. which leaves the array unmodified, since its size didnt change, but the value of the first element did. this.doc_types = response.data would assign to the array the value (probably an array). Commented Sep 3, 2018 at 12:16

1 Answer 1

3

I'm assuming when you say it doesn't work that trigger the listeners it should, not that the data structure itself doesn't get changed.

This is a noted caveat in Vue's change detection.

The solution is to use Vue's workaround

Vue.set(array, indexOfItem, newValue)

Or in your case

Vue.set(this.doc_types, 0, response.data)

This is explained more fully here: https://v2.vuejs.org/v2/guide/list.html#Caveats

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.