Element UI 使用指南
2024/4/1大约 5 分钟
Element UI 使用指南
概述
Element UI 是一个基于 Vue.js 的桌面端组件库,提供了丰富的组件用于构建企业级应用界面。
1. 安装与配置
1.1 安装 Element Plus(Vue 3 版本)
# 安装 Element Plus
npm install element-plus
# 安装图标库
npm install @element-plus/icons-vue1.2 全局配置
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')1.3 按需导入(推荐)
# 安装按需导入插件
npm install -D unplugin-vue-components unplugin-auto-import// vite.config.js
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({
plugins: [
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
})2. 基础组件
2.1 按钮
<template>
<div>
<el-button>默认按钮</el-button>
<el-button type="primary">主要按钮</el-button>
<el-button type="success">成功按钮</el-button>
<el-button type="warning">警告按钮</el-button>
<el-button type="danger">危险按钮</el-button>
<el-button size="small">小按钮</el-button>
<el-button size="default">默认大小</el-button>
<el-button size="large">大按钮</el-button>
<el-button :disabled="true">禁用按钮</el-button>
<el-button loading>加载中</el-button>
</div>
</template>2.2 输入框
<template>
<div>
<el-input
v-model="inputValue"
placeholder="请输入内容"
:disabled="false"
:clearable="true"
/>
<el-input
v-model="password"
type="password"
placeholder="请输入密码"
/>
<el-input
v-model="textarea"
type="textarea"
:rows="3"
placeholder="多行文本输入"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const inputValue = ref('')
const password = ref('')
const textarea = ref('')
</script>2.3 选择器
<template>
<div>
<!-- 下拉选择 -->
<el-select v-model="selectedValue" placeholder="请选择">
<el-option label="选项一" value="1" />
<el-option label="选项二" value="2" />
<el-option label="选项三" value="3" />
</el-select>
<!-- 多选 -->
<el-select v-model="selectedValues" multiple placeholder="请选择">
<el-option label="选项一" value="1" />
<el-option label="选项二" value="2" />
<el-option label="选项三" value="3" />
</el-select>
<!-- 级联选择 -->
<el-cascader
v-model="cascaderValue"
:options="options"
placeholder="请选择"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const selectedValue = ref('')
const selectedValues = ref([])
const cascaderValue = ref([])
const options = [
{
value: 'zhinan',
label: '指南',
children: [
{ value: 'shezhi', label: '设置' },
{ value: 'biaozhun', label: '标准' }
]
}
]
</script>3. 数据展示
3.1 表格
<template>
<el-table :data="tableData" border>
<el-table-column prop="name" label="姓名" />
<el-table-column prop="age" label="年龄" />
<el-table-column prop="email" label="邮箱" />
<el-table-column prop="status" label="状态">
<template #default="scope">
<el-tag :type="scope.row.status === 'active' ? 'success' : 'warning'">
{{ scope.row.status === 'active' ? '活跃' : '离线' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作">
<template #default="scope">
<el-button size="small" @click="edit(scope.row)">编辑</el-button>
<el-button size="small" type="danger" @click="delete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script setup>
import { ref } from 'vue'
const tableData = ref([
{ name: 'Blogger', age: 30, email: 'user@example.com', status: 'active' },
{ name: '张三', age: 25, email: 'zhangsan@example.com', status: 'offline' },
{ name: '李四', age: 28, email: 'lisi@example.com', status: 'active' }
])
function edit(row) {
console.log('编辑:', row)
}
function deleteRow(row) {
console.log('删除:', row)
}
</script>3.2 分页
<template>
<div>
<el-table :data="tableData" border>
<!-- 表格列定义 -->
</el-table>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 30, 40]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
</template>
<script setup>
import { ref } from 'vue'
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
function handleSizeChange(val) {
pageSize.value = val
// 重新获取数据
}
function handleCurrentChange(val) {
currentPage.value = val
// 重新获取数据
}
</script>4. 表单验证
4.1 基础表单
<template>
<el-form
:model="form"
:rules="rules"
ref="formRef"
label-width="100px"
>
<el-form-item label="用户名" prop="username">
<el-input v-model="form.username" />
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="form.password" type="password" />
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="form.email" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">提交</el-button>
<el-button @click="resetForm">重置</el-button>
</el-form-item>
</el-form>
</template>
<script setup>
import { ref, reactive } from 'vue'
const formRef = ref(null)
const form = reactive({
username: '',
password: '',
email: ''
})
const rules = {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, message: '密码长度至少 6 位', trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
]
}
function submitForm() {
formRef.value.validate((valid) => {
if (valid) {
console.log('表单验证通过')
} else {
console.log('表单验证失败')
}
})
}
function resetForm() {
formRef.value.resetFields()
}
</script>5. 弹窗与提示
5.1 消息提示
<template>
<div>
<el-button @click="showSuccess">成功提示</el-button>
<el-button @click="showError">错误提示</el-button>
<el-button @click="showWarning">警告提示</el-button>
<el-button @click="showInfo">信息提示</el-button>
</div>
</template>
<script setup>
import { ElMessage } from 'element-plus'
function showSuccess() {
ElMessage.success('操作成功')
}
function showError() {
ElMessage.error('操作失败')
}
function showWarning() {
ElMessage.warning('警告信息')
}
function showInfo() {
ElMessage.info('提示信息')
}
</script>5.2 对话框
<template>
<div>
<el-button @click="dialogVisible = true">打开对话框</el-button>
<el-dialog
title="对话框标题"
v-model="dialogVisible"
:width="500px"
:before-close="handleClose"
>
<p>对话框内容</p>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="confirm">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
function handleClose(done) {
// 关闭前的处理
done()
}
function confirm() {
// 确认操作
dialogVisible.value = false
}
</script>6. 布局组件
6.1 容器布局
<template>
<el-container>
<el-header>头部</el-header>
<el-container>
<el-aside width="200px">侧边栏</el-aside>
<el-main>主内容区</el-main>
</el-container>
<el-footer>底部</el-footer>
</el-container>
</template>
<style>
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
}
.el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
}
</style>6.2 栅格系统
<template>
<el-row :gutter="20">
<el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
</el-row>
</template>
<style>
.grid-content {
border-radius: 4px;
min-height: 36px;
}
.bg-purple {
background: #99a9bf;
}
.bg-purple-light {
background: #d3dce6;
}
</style>7. 进阶用法
7.1 自定义主题
// 创建自定义主题文件
$primary-color: #409EFF;
$success-color: #67C23A;
$warning-color: #E6A23C;
$danger-color: #F56C6C;
// 覆盖 Element Plus 变量
@use 'element-plus/theme-chalk/src/common/var.scss' with (
$color-primary: $primary-color,
$color-success: $success-color,
$color-warning: $warning-color,
$color-danger: $danger-color
);7.2 国际化
// main.js
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
const app = createApp(App)
app.use(ElementPlus, {
locale: zhCn
})
app.mount('#app')总结
Element UI 提供了丰富的组件库,可以快速构建美观的企业级应用界面。掌握常用组件和配置方式是高效开发的关键。
作者:Blogger
日期:2024年4月1日