VUE给动态生成的el-checkbox添加v-model

需求描述:根据返回的数据,页面动态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', '');
})

官方文档
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章