当前位置:首页 > 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的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…