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>
相关推荐
余生H2 分钟前
前端科技新闻(WTN-3)React v19 引发 Cloudflare 异常事件复盘 - 一次序列化升级,如何影响全球边缘网络?
前端·科技·react.js
HIT_Weston4 分钟前
62、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(六)
前端·ubuntu·gitlab
ID_180079054737 分钟前
淘宝关键词搜索 API 系列 数据返回参考(附解析与实战)
java·服务器·前端
Hao_Harrision15 分钟前
50天50个小项目 (React19 + Tailwindcss V4) ✨| BackgroundSlider(背景滑块)
前端·typescript·react·vite7·tailwildcss
weixin_3077791322 分钟前
Jenkins Font Awesome API插件:现代化插件界面的图标引擎
开发语言·前端·自动化·jenkins
阿蒙Amon25 分钟前
JavaScript学习笔记:13.Promise
javascript·笔记·学习
小小心LOVE29 分钟前
Vue3 安装和使用 vue-office来实现 Word、Excel 和 PDF 文件的预览
vue.js·word·excel
June bug31 分钟前
【Vue】从0开始使用Vue构建界面
前端·vue.js·前端框架
爱吃大芒果31 分钟前
Flutter 动画实战:隐式动画、显式动画与自定义动画控制器
开发语言·javascript·flutter·ecmascript·gitcode
shuaijie051831 分钟前
在Vue.js中实现列表的拖动功能,使用第三方库如vuedraggable(基于Sortable.js)
android·javascript·vue.js