当前位置:首页 > VUE

vue 实现input封装

2026-02-19 15:47:03VUE

封装基础Input组件

创建一个基础的Input.vue文件,包含props接收父组件传递的参数:

<template>
  <input
    :type="type"
    :value="modelValue"
    @input="$emit('update:modelValue', $event.target.value)"
    :placeholder="placeholder"
    :disabled="disabled"
    class="base-input"
  />
</template>

<script>
export default {
  name: 'BaseInput',
  props: {
    type: {
      type: String,
      default: 'text'
    },
    modelValue: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style scoped>
.base-input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>

实现v-model支持

使用Vue3的v-model语法糖或Vue2的.sync修饰符:

<!-- Vue3写法 -->
<BaseInput v-model="username" />

<!-- Vue2写法 -->
<BaseInput :value.sync="username" />

添加表单验证功能

扩展组件支持验证状态显示:

vue 实现input封装

<template>
  <div class="input-wrapper">
    <input
      <!-- 原有属性 -->
      :class="{'error': error}"
    />
    <span v-if="error" class="error-message">{{ errorMessage }}</span>
  </div>
</template>

<script>
export default {
  props: {
    error: Boolean,
    errorMessage: String
  }
}
</script>

<style>
.error {
  border-color: red;
}
.error-message {
  color: red;
  font-size: 12px;
}
</style>

添加插槽增强灵活性

支持前缀/后缀插槽:

<template>
  <div class="input-container">
    <span v-if="$slots.prefix" class="prefix">
      <slot name="prefix"></slot>
    </span>
    <input <!-- 原有input --> />
    <span v-if="$slots.suffix" class="suffix">
      <slot name="suffix"></slot>
    </span>
  </div>
</template>

支持清除功能

添加清除按钮:

vue 实现input封装

<template>
  <div class="input-with-clear">
    <input <!-- 原有属性 --> />
    <button 
      v-if="showClear && modelValue" 
      @click="$emit('update:modelValue', '')"
      class="clear-btn"
    >×</button>
  </div>
</template>

<script>
export default {
  props: {
    showClear: {
      type: Boolean,
      default: false
    }
  }
}
</script>

完整示例组件

综合所有功能的完整组件:

<template>
  <div class="custom-input" :class="{'disabled': disabled}">
    <label v-if="label" class="input-label">{{ label }}</label>
    <div class="input-group">
      <slot name="prefix"></slot>
      <input
        :type="type"
        :value="modelValue"
        @input="handleInput"
        @blur="$emit('blur')"
        @focus="$emit('focus')"
        :placeholder="placeholder"
        :disabled="disabled"
        :class="{'error': error}"
      />
      <button 
        v-if="clearable && modelValue" 
        @click="handleClear"
        class="clear-btn"
      >×</button>
      <slot name="suffix"></slot>
    </div>
    <span v-if="error && errorMessage" class="error-message">
      {{ errorMessage }}
    </span>
  </div>
</template>

<script>
export default {
  name: 'CustomInput',
  props: {
    type: { /* 同上 */ },
    modelValue: { /* 同上 */ },
    label: String,
    clearable: Boolean,
    // 其他props...
  },
  methods: {
    handleInput(e) {
      this.$emit('update:modelValue', e.target.value)
    },
    handleClear() {
      this.$emit('update:modelValue', '')
      this.$emit('clear')
    }
  }
}
</script>

<style scoped>
/* 完整样式 */
.custom-input {
  margin-bottom: 16px;
}
.input-group {
  position: relative;
  display: flex;
  align-items: center;
}
.clear-btn {
  position: absolute;
  right: 8px;
  background: transparent;
  border: none;
  cursor: pointer;
}
/* 其他样式... */
</style>

全局注册组件

在main.js中全局注册:

import CustomInput from '@/components/CustomInput.vue'

// Vue3
app.component('CustomInput', CustomInput)

// Vue2
Vue.component('CustomInput', CustomInput)

标签: vueinput
分享给朋友:

相关文章

vue实现单词反转

vue实现单词反转

实现思路 在Vue中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过计…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现border样式

vue实现border样式

实现边框样式的方法 在Vue中实现边框样式可以通过多种方式完成,包括内联样式、CSS类绑定、动态样式绑定等。以下是几种常见的方法: 内联样式绑定 通过style属性直接绑定样式对象,适用于简单的边…