最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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 .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.

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/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.

Share Improve this question asked Feb 7, 2016 at 13:26 Sohrab TaeeSohrab Taee 7601 gold badge10 silver badges30 bronze badges 1
  • There's not enough information to figure out what is wrong. Can you recreate this in a fiddle? – David K. Hess Commented Feb 7, 2016 at 17:50
Add a ment  | 

2 Answers 2

Reset to default 13

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.

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 plicated solutions out there.

发布评论

评论列表(0)

  1. 暂无评论