1

For some reason, pushing data into an array doesnt update the view/vue dev tools data. Why?

I have cleaned the browser cache without any luck.

theArray = [
   [
      {
         test: true
      }
   ]
]

methods: {
   addNewItem() {
     console.log(this.theArray[0].length); // = 1

     this.theArray[0].push({}); 

     console.log(this.theArray[0].length); // = 2. This does however show that the data was pushed but why do the view and vuejs tools not update/show the pushed data?
   }
}

However, doing this works for some reason?

this.theArray.push({}); 

3
  • Can you please share a minimum example of the working code? Commented Dec 21, 2021 at 13:18
  • Where is your theArray defined in data section? Commented Dec 21, 2021 at 13:55
  • Provide a minimal reproducible code. Commented Dec 21, 2021 at 13:55

1 Answer 1

1

That's due to Vue's reactivity. You can use Vue.set()/this.$set().

this.$set(theArray, 0, [ ...theArray[0], {} ]);

https://v2.vuejs.org/v2/guide/reactivity.html#For-Arrays

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

2 Comments

That doesnt work either. Gives same problem
Maybe theArray[0] isn't reactive either … 🤔

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.