- el-select的multiple初始值设置为false单选
- 选中性别
- 点击按钮切换multiple为true,选择框的值selectValue设置为[],但是页面上选择框的值并没有清空
操作
image
实际结果:
image
理想结果
image
<!-- 选择框 -->
<el-select v-model="selectValue" :multiple="multiple">
<el-option v-for="el in sexOptions" :key="el.key" :label="el.label" :value="el.value"></el-option>
</el-select>
<!-- 修改选择框类型,单选/多选 -->
<el-button type="primary" @click="changeSelectType">切换为多选</el-button>
export default {
data() {
return {
multiple: false,
selectValue: undefined,
sexOptions: [{ key: 'woman', value: 'woman', label: '女' }, { key: 'man', value: 'man', label: '男' }]
}
},
methods: {
changeSelectType() {
// 如果当前选择框为单选,切换为多选,selectValue置为空数组[]
this.multiple = true
this.selectValue = []
}
}
}
解决办法,先把selectValue置为undefined, 再在nextTick里面切换multiple为true,把selectValue设置为[]
changeSelectType() {
// 先把selectValue置为undefined
// 再在nextTick里面切换multiple为true,把selectValue设置为[]
this.selectValue = undefined
this.$nextTick(() => {
this.multiple = true
this.selectValue = []
})
}