element el-input 二次封装

说明:为实现输入限制,不可输入空格,长度限制。

inputView.vue

html 复制代码
<template>
  <!-- 输入框 -->
  <el-input
    :type="type"
    :placeholder="placeholder"
    v-model="input"
    @input="inputChange"
    :maxlength="maxlength"
  ></el-input>
</template>
<script>
export default {
  props: {
    type: {
      type: String,
      default: "text",
    },
    value: {
      type: [String, Number],
      default() {
        return "";
      },
    },
    maxlength: {
      type: Number,
      default: 30,
    },
    placeholder: {
      type: String,
      default: "",
    },
  },
  watch: {
    value: {
      handler(val) {
        this.input = val;
      },
      deep: true,
    },
  },
  data() {
    return {
      input: this.value,
    };
  },
  methods: {
    inputChange(value) {
      this.input = value?.replace(/\s/g, "");
      if (this.type === "num") {
       // 做数字型的判断,因为采用input 的 Number 类型,最大值还得做单独匹配,偷懒,所以用了num代替
        this.input = Number(value?.replace(/\D/g, ""));
      }
      this.$emit("input", this.input);
    },
  },
};
</script>

全局注册

components/index.js

main.js 中引入

javascript 复制代码
import Components from '@/components'
Vue.use(Components)

组件中使用

html 复制代码
<inputView
  v-model="propertyForm.count"
  style="width: 80px; margin: 0 10px"
  type="num"
  :maxlength="5"
></inputView>
 
<inputView
  v-model="formInline.riskName"
  placeholder="请输入"
></inputView>
相关推荐
坚持学习前端日记13 分钟前
AI 产品开发经验
前端·javascript·人工智能·visual studio
张一凡9315 分钟前
easy-model:简化领域驱动开发的理想选择
前端·react.js
雾削木21 分钟前
STM32输入捕获测量PWM频率占空比
前端·javascript·stm32
weixin1997010801621 分钟前
淘宝客商品详情页前端性能优化实战
java·前端·python·性能优化
JamesYoung797125 分钟前
第八部分 — UI 表面 动作(工具栏)、徽标、弹出窗口
前端·javascript
Joker Zxc27 分钟前
【前端基础(Javascript部分)】5、JavaScript的循环语句
开发语言·前端·javascript
Neweee1 小时前
JavaScript进阶内容详解
前端
大鸡爪1 小时前
Vue3 组件库实战(五):Icon 图标组件的设计与实现
前端·vue.js
bluceli1 小时前
前端测试实战指南:构建高质量代码的完整体系
前端·测试