如果您想在el-select中显示空值,可以使用v-if指令或者三元表达式来判断接口返回的值是否为空,然后将其赋值为一个空数组或空对象,如下所示:

<el-select v-model="selectedValue">
  <el-option
    v-for="item in optionList"
    :key="item.value"
    :label="item.label"
    :value="item.value">
  </el-option>
</el-select>
export default {
  data() {
    return {
      selectedValue: '',
      optionList: []
    }
  },
  created() {
    this.fetchOptionList()
  },
  methods: {
    fetchOptionList() {
      // 模拟接口返回值为空的情况
      // const res = []
      const res = [{ label: '选项1', value: '1' }, { label: '选项2', value: '2' }]
      if (res.length > 0) {
        this.optionList = res
        this.selectedValue = res[0].value
      } else {
        // 接口返回值为空时将optionList赋值为空数组或空对象
        this.optionList = []
        // this.optionList = {}
        this.selectedValue = ''
      }
    }
  }
}
``
vue 当接口返回的值在el-select中没有时显示空

原文地址: https://www.cveoy.top/t/topic/fXK3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录