当前位置:首页 > VUE

vue 实现input封装

2026-01-18 23:24:28VUE

封装 Input 组件的基本实现

在 Vue 中封装 Input 组件可以通过创建一个可复用的自定义组件来实现,以下是一个基础示例:

创建 Input 组件文件 (BaseInput.vue)

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

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

<style scoped>
.base-input {
  margin-bottom: 15px;
}
.base-input label {
  display: block;
  margin-bottom: 5px;
}
.base-input input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>

使用 v-model 支持

Vue 3 使用 modelValue 作为默认 prop 和 update:modelValue 作为默认事件来实现 v-model:

父组件中使用

<template>
  <div>
    <BaseInput v-model="username" label="用户名" placeholder="请输入用户名" />
    <BaseInput v-model="password" type="password" label="密码" />
  </div>
</template>

<script>
import BaseInput from './BaseInput.vue'

export default {
  components: { BaseInput },
  data() {
    return {
      username: '',
      password: ''
    }
  }
}
</script>

添加验证功能

可以扩展组件以支持验证功能:

vue 实现input封装

带验证的 Input 组件

<template>
  <div class="base-input">
    <label v-if="label">{{ label }}</label>
    <input
      :type="type"
      :value="modelValue"
      @input="handleInput"
      @blur="validate"
      :placeholder="placeholder"
      :disabled="disabled"
      :class="{ 'error': error }"
    />
    <span v-if="error" class="error-message">{{ error }}</span>
  </div>
</template>

<script>
export default {
  props: {
    // ...原有props
    rules: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      error: ''
    }
  },
  methods: {
    handleInput(e) {
      this.$emit('update:modelValue', e.target.value)
      this.error = ''
    },
    validate() {
      if (this.rules.length) {
        const failedRule = this.rules.find(rule => !rule.validator(this.modelValue))
        this.error = failedRule ? failedRule.message : ''
      }
    }
  }
}
</script>

<style scoped>
.error {
  border-color: red;
}
.error-message {
  color: red;
  font-size: 12px;
  display: block;
  margin-top: 5px;
}
</style>

使用验证规则

<BaseInput
  v-model="email"
  label="邮箱"
  :rules="[
    { validator: val => !!val, message: '邮箱不能为空' },
    { validator: val => /.+@.+\..+/.test(val), message: '请输入有效的邮箱' }
  ]"
/>

支持插槽增强灵活性

可以通过插槽让组件更灵活:

vue 实现input封装

带插槽的 Input 组件

<template>
  <div class="base-input">
    <slot name="label">
      <label v-if="label">{{ label }}</label>
    </slot>
    <div class="input-wrapper">
      <slot name="prefix"></slot>
      <input
        :type="type"
        :value="modelValue"
        @input="$emit('update:modelValue', $event.target.value)"
        :placeholder="placeholder"
        :disabled="disabled"
      />
      <slot name="suffix"></slot>
    </div>
    <slot name="message">
      <span v-if="error" class="error-message">{{ error }}</span>
    </slot>
  </div>
</template>

<style scoped>
.input-wrapper {
  display: flex;
  align-items: center;
}
</style>

使用插槽示例

<BaseInput v-model="searchText" placeholder="搜索...">
  <template #prefix>
    <SearchIcon />
  </template>
  <template #suffix>
    <button @click="clearSearch">清除</button>
  </template>
</BaseInput>

响应式设计考虑

可以添加响应式属性来控制不同屏幕尺寸下的样式:

响应式样式

<style scoped>
.base-input {
  margin-bottom: 15px;
}

@media (max-width: 768px) {
  .base-input {
    margin-bottom: 10px;
  }

  .base-input input {
    padding: 6px 8px;
  }
}
</style>

标签: vueinput
分享给朋友:

相关文章

vue实现预览

vue实现预览

Vue实现文件预览的方法 使用Vue实现文件预览功能可以通过多种方式完成,具体取决于文件类型(图片、PDF、视频等)。以下是几种常见文件类型的实现方案。 图片预览 通过<input type…

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供的…

项目基于vue实现

项目基于vue实现

基于Vue实现项目的关键方法 搭建基础环境 安装Node.js和npm/yarn,确保开发环境具备运行Vue的能力。通过Vue CLI快速初始化项目结构,选择所需配置(如Babel、Router、Vu…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <t…

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…