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>
相关推荐
小王和八蛋3 分钟前
前端存储与离线应用实战:Cookie、LocalStorage、PWA 及 Service Worker 核心知识点
前端·javascript
JarvanMo6 分钟前
终极指南:在 Flutter 中通过 sign_in_with_apple 实现 Apple 登录
前端
Learner12 分钟前
Python异常处理
java·前端·python
tao35566716 分钟前
VS Code登录codex,报错(os error 10013)
java·服务器·前端
军军君0119 分钟前
Three.js基础功能学习七:加载器与管理器
开发语言·前端·javascript·学习·3d·threejs·三维
哈__20 分钟前
React Native 鸿蒙开发:内置 Share 模块实现无配置社交分享
javascript·react native·react.js
JarvanMo22 分钟前
情迷服务器驱动 UI:我在 Flutter 开发中的爱与哀愁
前端
tzy23324 分钟前
分享一个 HTTP(S) 代理&抓包工具,拦截和Mock Web客户端请求和服务端响应
前端·网络协议·http
代码小学僧30 分钟前
普通前端仔的 2025 : 年终总结与 AI 对我的影响
前端·程序员·ai编程
Mike_jia36 分钟前
TCP 粘包/拆包问题
前端