vue.js - Vue.js 更新数组无法使用 this.$set
问题描述
我向服务发出请求并填充一个内部有多个数组的对象,它本身就是一个数组。前任:this.Jprojs: [{name : 'test', ListItem1 : [], ListItem2 : [] }]
我把那个对象放在一个 v-for 中:
<div id="app">
<table class="table table-striped">
<tr>
<th width="15%">Proj</th>
<th>Detail</th>
</tr>
<tr v-for="proj in Jprojs" :key="proj.name">
<td style="vertical-align:middle;"><strong>{{proj.name}}</strong><br/><a v-on:click="list(proj)"> <font-awesome-icon icon="tasks" /></a></td>
<td>{{proj.ListItem1.length}}</td>
</tr>
</table>
我有方法列表:
list : function(proj){
axios.get(url).then(
response => {
this.$set(proj.ListItem1,0,response.data.value);
//Vue.set(proj.ListItem1,0,response.data.value);
this.nextTick;
console.log(proj)
},
error => {
},
err => { }
);
}
控制台显示更新,但 html 未更新。
解决方案
确保更新Jprojs
值,而不是proj
. 您可以传递索引而不是proj
对象。
获取索引v-for="(proj, index) in Jprojs"
并将其作为list(index)
. 然后只需编辑Jprojs
具有给定索引的数组;Jprojs[index].ListItem1 = ...
推荐阅读
- javascript - BinarySearchTree 在 javascript 中的插入函数
- java - 如何使用java将数据集的两行组合成spark中的一行
- c++ - MacOS 上的 Boost ASIO IPv6 多播示例错误:无法分配请求的地址
- node.js - Sinon 存根在测试文件之外被忽略
- powerbi - 如何在 Power BI 中计算累计和
- r - 无法在 MacOS Mojave 和 R 3.5.2 上加载 rJava
- javascript - 如何处理 Javascript 异步
- python - 来自多个列表的元素的最小组合,使得所有元素至少出现一次
- c++ - 在 C++ 中区分标记化字符串中的操作数和运算符
- wpf - 获取绑定到 LINQ 查询结果的 DataGrid 中 DataGridTemplateColumn 的编辑值