elementui写一个自定义的rangeInput的组件

组件定义

  • 使用el-row确保元素都在一行上
  • 对外暴露的prop是minValue和maxValue,但是不建议直接使用,使用计算属性minValueComputed和maxValueComputed
  • 更改计算属性的值的不要直接更改计算属性,也不要直接更改原本的prop,通知外层的父组件来更改,通过父组件将变动传递到子组件中
  • 子组件使用prop进行属性传递的时候,应该极力避免在子组件中直接更改prop的值,获取属性值使用计算属性进行v-model绑定,不要直接绑定在prop上,因为v-model的数据流是双向的。修改值的话,监听计算属性的setter方法,当变化的时候,通知父组件键更改外部的prop的传递值
html 复制代码
<template>
  <el-row>
    <el-col :span="10">
      <el-input
        type="number"
        v-model="minValueComputed"
        :placeholder="minPlaceholder"
        size="mini"
        class="rangeInput"
        @input="minValueComputed = handleInput(minValueComputed)"
      />
    </el-col>
    <el-col :span="4">
      <span style="text-align: center; display: block; margin-left: 5px">至</span>
    </el-col>
    <el-col :span="10">
      <el-input
        type="number"
        v-model="maxValueComputed"
        :placeholder="maxPlaceholder"
        size="mini"
        class="rangeInput"
        @input="maxValueComputed = handleInput(maxValueComputed)"
      />
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: 'RangeInput',
  props: {
    minValue: String||Number,
    maxValue: String||Number,
    minPlaceholder: String,
    maxPlaceholder: String,
    cleanFlag: Boolean
  },
  data() {
    return {
      internalMinValue: this.minValue,
      internalMaxValue: this.maxValue,
      minErrorMsg: ''
    }
  },
  computed: {
    minValueComputed: {
      get() {
        return this.internalMinValue;
      },
      set(value) {
        this.internalMinValue = value;
        this.$emit('update:minValue', value);
      }
    },
    maxValueComputed: {
      get() {
        return this.internalMaxValue;
      },
      set(value) {
        this.internalMaxValue = value;
        this.$emit('update:maxValue', value);
      }
    }
  },
  methods: {
    handleInput(value) {
      return value.replace(/[^\d|\.|-]/g, '');
    },
  },
  watch: {
    cleanFlag() {
        this.minValueComputed = '';
        this.maxValueComputed = '';
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .rangeInput .el-input__inner{
  width: 70px !important;
  height: 30px !important;
}



</style>


<style scoped>
/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
/deep/ input[type="number"] {
  -moz-appearance: textfield;
}
/deep/ inpit {
  border: none
}
</style>

使用

给外围的el-form中加上一个inline-block,最大值和最小值不在同一行的情况

html 复制代码
<el-form-item label="损耗比率(%)" prop="wastageRate" >
  <range-input
    :minValue.sync="wastageRateMin"
    :maxValue.sync="wastageRateMax"
    minPlaceholder="最小值"
    maxPlaceholder="最大值"
    :cleanFlag="rankInputCleanFlag"
  />
</el-form-item>
相关推荐
雪碧聊技术1 小时前
前端VUE3项目部署到linux服务器(CentOS 7)
前端·linux部署vue3项目
酒尘&7 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要7 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569158 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569158 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v9 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式10 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw510 小时前
npm几个实用命令
前端·npm
!win !10 小时前
npm几个实用命令
前端·npm
代码狂想家11 小时前
使用openEuler从零构建用户管理系统Web应用平台
前端