当前位置:首页 > 前端教程

elementui input

2026-03-06 08:17:21前端教程

ElementUI Input 组件使用指南

ElementUI 的 Input 组件是常用的表单输入控件,支持多种输入类型和交互功能。以下是核心用法和特性:

基础用法

通过 v-model 实现双向数据绑定:

<el-input v-model="inputValue" placeholder="请输入内容"></el-input>

数据定义:

data() {
  return {
    inputValue: ''
  }
}

输入类型控制

通过 type 属性指定输入类型:

  • text(默认)
  • textarea(多行文本)
  • password(密码输入,显示切换按钮)
    <el-input type="password" v-model="password" show-password></el-input>

禁用与只读状态

<el-input v-model="inputValue" disabled></el-input>
<el-input v-model="inputValue" readonly></el-input>

带图标的输入框

使用 prefix-iconsuffix-icon 属性:

elementui input

<el-input 
  v-model="inputValue"
  prefix-icon="el-icon-search"
  placeholder="搜索内容">
</el-input>

输入长度限制

结合 maxlengthshow-word-limit 显示字数统计:

<el-input
  v-model="inputValue"
  maxlength="100"
  show-word-limit
  type="textarea">
</el-input>

复合型输入框

添加前置或后置内容:

<el-input v-model="inputValue">
  <template slot="prepend">Http://</template>
</el-input>

<el-input v-model="inputValue">
  <template slot="append">.com</template>
</el-input>

输入验证

结合 Form 组件进行验证:

elementui input

<el-form :model="form" :rules="rules">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="form.username"></el-input>
  </el-form-item>
</el-form>

验证规则:

rules: {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
  ]
}

自定义模板

通过插槽自定义内容:

<el-input v-model="inputValue">
  <template slot="suffix">
    <i class="el-icon-date"></i>
  </template>
</el-input>

事件处理

常用事件包括:

  • @change:值变化时触发(失焦或回车)
  • @input:输入时实时触发
  • @focus/@blur:聚焦/失焦时触发
    <el-input 
    v-model="inputValue"
    @change="handleChange"
    @input="handleInput">
    </el-input>

样式定制

通过 CSS 覆盖默认样式:

.el-input__inner {
  border-radius: 4px;
}
.custom-input .el-input__inner {
  background-color: #f5f7fa;
}

标签: elementuiinput
分享给朋友:

相关文章

elementui响应式布局

elementui响应式布局

响应式布局基础概念 响应式布局指页面能够根据屏幕尺寸自动调整结构和样式,确保在不同设备上呈现良好的用户体验。Element UI 基于 Vue.js,其组件默认支持响应式设计,但需结合 CSS 媒体查…

vue elementui

vue elementui

Vue 中使用 Element UI Element UI 是一套基于 Vue 2.0 的桌面端组件库,提供丰富的 UI 组件和交互功能。以下是如何在 Vue 项目中集成和使用 Element UI…

elementui transfer

elementui transfer

ElementUI Transfer 组件使用指南 ElementUI 的 Transfer 组件用于在两栏之间转移数据,适用于权限分配、数据分类等场景。以下为详细使用方法: 基础用法 在模板中引…

elementui渐变

elementui渐变

实现ElementUI渐变效果的方法 ElementUI组件库本身不直接提供渐变样式,但可以通过CSS自定义实现渐变效果。以下是几种常见实现方式: 背景渐变 通过CSS的linear-gradien…

elementui备注

elementui备注

使用 Element UI 添加备注功能 Element UI 提供了多种组件可以用来实现备注功能,具体选择取决于需求场景。以下是几种常见的实现方式: 输入框备注 使用 el-input 组件创建…

移植elementui

移植elementui

移植 Element UI 的方法 Element UI 是一个基于 Vue.js 的组件库,移植通常涉及从旧项目迁移到新项目或升级版本。以下是常见的移植方法: 安装 Element UI 在目标…