※前提条件:本文基于 Vue 2.0 创作
#codeprettify{{
<el-checkbox-group v-model="checked" size="medium">
<el-checkbox-button v-for="city in cities" :label="city" :key="city" @change="checkbox(city)">{{ city }}
</el-checkbox-button>
/el-checkbox-group>
const cityOptions = ['上海', '北京', '广州', '深圳']; export default {
data () { return { checked: ['上海'],//不能为null,必须要有值 cities: cityOptions }; }
} }}
不能直接this.checked=[]或者等于null。因为绑定之后的数组里面有特定参数; 不能直接把this.checked=city 这就需要使用到“includes/包含”这个函数判断包含不包含,再用三运算符把当前值给进去
checkbox(city) { this.checked = this.checked.includes(city) ? [city] : []; },
コメント: