简体   繁体   English

更新v-for中使用的数组内的对象时的Vue js反应性问题

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

I have an application using vue.js. 我有一个使用vue.js的应用程序。 I generate a list of results using v-for in an array of objects. 我在对象数组中使用v-for生成结果列表。 when I update the object inside nth item in array, using underscore _.extend, the view of project does not update. 当我更新数组中第n项内的对象时,使用下划线_.extend,项目视图不会更新。 There is a solution for this problem at http://vuejs.org/guide/reactivity.html which indicates to use _.extend like this: http://vuejs.org/guide/reactivity.html有一个解决此问题的方法,它表示使用_.extend,如下所示:

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. 但问题是当我使用像它说的扩展时,它不会更新视图。

Vue is unable to detect the change when you set a new item by array index. 按数组索引设置新项目时,Vue无法检测到更改。 To get around this, you can use the $set() method that Vue adds to the array. 要解决此问题,您可以使用Vue添加到数组的$set()方法。

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. 我通过确保包含v-for的标签被标签包装,解决了我在v-for中没有更新的问题。

It had been wrapped in a v-ons-list. 它被包装在一个v-ons-list中。 And I could imagine some people wrapping it in a div tag. 我可以想象有些人将它包装在div标签中。 This is a quick possibility to eliminate before trying all the other more complicated solutions out there. 在尝试所有其他更复杂的解决方案之前,这是一种快速消除的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM