el-select 的multiple属性从false切换为true时页面值不清空

  1. el-select的multiple初始值设置为false单选
  2. 选中性别
  3. 点击按钮切换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 = []
      })
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容