"解决 Element UI Select 兼容性问题:基于原生 select + v-model 的实现"

61 阅读1分钟
<!--
  * @author: yxm
  * @description:原生select v-model版本
  * 用法: <ComSelectJsIndexModel placeholder="请选择退课类型" v-model="refundCourseType" :optionsData="options" />
-->
<template>
  <div>
    <select
      class="selectDom"
      :placeholder="placeholder"
      :value="selectedValue"
      @input="handleInput">
      <option disabled selected value="">{{ placeholder }}</option>
      <option
        v-for="(item, index) in optionsData"
        :key="index"
        :value="item.value">
        {{ item.label }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    optionsData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      selectedValue: this.value
    };
  },
  watch: {
    value(newVal) {
      this.selectedValue = newVal;
    }
  },
  methods: {
    handleInput(e) {
      const value = e.target.value;
      this.selectedValue = value;
      this.$emit('input', value);
    }
  }
};
</script>

<style lang="scss" scoped>
.selectDom {
  width: 100%;
  height: 36px;
  padding-left: 9px;
  padding-right: 15px;
  color: #606266;
  border: 1px solid #DCDFE6;
  border-radius: 3px;
}
.selectDom2{
  border-color: #F56C6C;
}

select:focus {
  border: 1px solid #409eff;
  outline: none;
  border-color: #4a90e2;
  border-width: 1px;
  border-style: solid;
}
</style>


refundCourseType:会自动获取到下拉选择的值