vue2 组件封装 el-input
customInput
index.vue. // 组件页面
index.js. 全局注册使用
index.vue
<template> <el-input v-bind="$attrs" v-on="$listeners" :value="value" @input="handleInput" :placeholder="placeholder" :type="type" clearable :disabled="disabled" show-word-limit > <!-- 透传所有具名插槽和默认插槽 --> <template v-for="(slot, name) in $slots" :slot="name"> <slot :name="name"></slot> </template> </el-input> </template> <script> export default { name: "CustomInput", props: { value: { type: [String, Number], default: "", }, // 去除首尾空格 autoTrim: { type: Boolean, default: false, }, placeholder: { type: String, default: "请输入", }, clearable: { type: Boolean, default: true, }, type: { type: String, default: "text", }, disabled: { type: Boolean, default: false, }, // 是否显示字数统计 showWordLimit: { type: Boolean, default: false, }, }, computed: { // maxlength() { // return this.$attrs.maxlength; // } }, mounted(){ }, methods: { handleInput(value) { // 处理自定义逻辑,如 autoTrim const finalValue = this.autoTrim ? value.trim() : value; // 通过 emit 'input' 事件更新 v-model this.$emit("input", finalValue); // Vue 3 // this.$emit("update:modelValue", finalValue); }, }, }; </script>
index.js.
import CustomInput from './index.vue'; CustomInput.install = function(Vue) { Vue.component(CustomInput.name, CustomInput); }; export default CustomInput;
//main.js 注册全局。vue.use(custominput)
页面使用
<el-form-item label="输入框" prop="name"> <my-input v-model="ruleForm.name" placeholder="请输入哈哈" > <i slot="suffix" class="el-input__icon el-icon-date"></i> </my-input> </el-form-item>

浙公网安备 33010602011771号