Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue js reactivity issue when updating objects inside array used in v-for

I have an application using vue.js. I generate a list of results using v-for in an array of objects. when I update the object inside nth item in array, using underscore _.extend, the view of project does not update. There is a solution for this problem at http://vuejs.org/guide/reactivity.html which indicates to use _.extend like this:

this.results.displayed[key] = _.extend({}, this.results.displayed[key], detail.items);

but the problem is when I use extend like it said, it does not update the view.

like image 603
Sohrab Taee Avatar asked Feb 07 '16 13:02

Sohrab Taee


2 Answers

Vue is unable to detect the change when you set a new item by array index. To get around this, you can use the $set() method that Vue adds to the array.

var newObject = _.extend({}, this.results.displayed[key], detail.items);
this.results.displayed.$set(key, newObject);

More info here.

like image 75
Peter Avatar answered Oct 20 '22 15:10

Peter


I solved my problem of items in v-for not updating, by making sure that the tag containing the v-for was wrapped by a tag.

It had been wrapped in a v-ons-list. And I could imagine some people wrapping it in a div tag. This is a quick possibility to eliminate before trying all the other more complicated solutions out there.

like image 28
Yann Stoneman Avatar answered Oct 20 '22 15:10

Yann Stoneman