需求描述:根据返回的数据,页面动态for循环添加el-checkbox,动态绑定v-model;
html代码:
<el-checkbox
class="all-choose"
v-for="(item,index) in tabList"
:key="index"
v-model="item.status"
@change="ChooseAll(index,item.status)">
全选
</el-checkbox>
tabList数据格式:
tabList: [
{
id: '1',
name: 'test'
},
{
id: '2',
name: 'teste2'
}
]
之前的错误做法:
tabList.forEach((item, index) => {
item.status = false;
})
开始以为这样就可以了,毕竟在el-checkbox上用item.status绑定v-model,后面发现checkbox点击的时候并没有更新;
解决方案:
import Vue from 'vue'
tabList.forEach((item, index) => {
// item.status = false;
Vue.set(item, 'status', '');
})