ElementPlus是Element UI的Vue 3版本,提供了丰富的桌面端组件集,方便开发者构建美观、易用的Web应用。它支持模块化设计、响应式布局和国际化,具有良好的性能和可维护性。ElementPlus还提供了详尽的文档和活跃的社区支持,帮助开发者快速上手和解决问题。
ElementPlus简介ElementPlus是Element UI的Vue 3版本,它是一个基于Vue 3的桌面端组件库,提供了一套完善的UI组件集,可以方便地帮助开发者构建美观、易用、响应式的Web应用。ElementPlus继承了Element UI的众多优点,并且针对Vue 3进行了优化,使得开发者可以更便捷地使用现代化的前端技术。
安装ElementPlus有两种方式:
使用npm安装
npm install element-plus --save
<link rel="stylesheet" href="https://unpkg.com/element-plus/lib/index.css"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="https://unpkg.com/vue@next"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="https://unpkg.com/element-plus"></script>
在Vue项目中使用ElementPlus,需要进行一些配置步骤:
全局引入
// main.js import { createApp } from 'vue'; import App from './App.vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(App); app.use(ElementPlus); app.mount('#app');
按需引入
// babel-plugin-component配置 { "libraryName": "element-plus", "style": true }
// webpack.config.js module.exports = { //... resolve: { alias: { 'element-ui': 'element-plus', 'element-ui/lib/theme-chalk/index.css': 'element-plus/lib/theme-chalk/index.css' } } };
在Vue项目中,可以通过以下步骤快速开始使用ElementPlus:
引入组件
<template> <el-button type="primary">按钮</el-button> </template> <script> import { defineComponent } from 'vue'; import { ElButton } from 'element-plus'; export default defineComponent({ components: { ElButton } }); </script>
使用组件
<template> <el-button type="primary">按钮</el-button> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ components: { ElButton: () => import('element-plus/packages/button') } }); </script>
Button组件用于创建按钮,支持多种类型和样式。
<template> <el-button type="primary">主要按钮</el-button> <el-button type="success">成功按钮</el-button> <el-button type="info">信息按钮</el-button> <el-button type="warning">警告按钮</el-button> <el-button type="danger">危险按钮</el-button> </template>
<template> <el-button type="primary" icon="el-icon-edit">编辑</el-button> <el-button type="success" icon="el-icon-check">成功</el-button> <el-button type="info" icon="el-icon-message">信息</el-button> <el-button type="warning" icon="el-icon-star-off">警告</el-button> <el-button type="danger" icon="el-icon-delete">删除</el-button> </template>
<template> <el-button type="text">文本按钮</el-button> </template>
<template> <el-button-group> <el-button type="primary" icon="el-icon-edit">编辑</el-button> <el-button type="primary" icon="el-icon-share">分享</el-button> <el-button type="primary" icon="el-icon-delete">删除</el-button> </el-button-group> </template>
Input组件用于创建输入框,支持文本输入、密码输入等多种类型。
<template> <el-input placeholder="请输入内容" v-model="input" clearable></el-input> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const input = ref(''); return { input }; } }); </script>
<template> <el-input placeholder="请输入内容" v-model="input" clearable></el-input> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const input = ref(''); return { input }; } }); </script>
<template> <el-input v-model="input" placeholder="请输入内容"></el-input> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const input = ref(''); return { input }; } }); </script>
Table组件用于创建表格,支持数据展示、排序、分页等功能。
<template> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const tableData = ref([ { date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' } ]); return { tableData }; } }); </script>
<template> <el-table :data="tableData" style="width: 100%" @cell-click="handleCellClick"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const tableData = ref([ { date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' } ]); const handleCellClick = (row, column, cell, event) => { console.log(row, column, cell, event); }; return { tableData, handleCellClick }; } }); </script>
Message组件用于在页面中显示消息提示框,支持多种类型的消息通知。
import { message } from 'element-plus'; message('这是一条消息提示');
import { message } from 'element-plus'; message.success('这是一条成功提示'); message.info('这是一条信息提示'); message.warning('这是一条警告提示'); message.error('这是一条错误提示');
import { message } from 'element-plus'; message({ message: '这是一条带自定义内容的消息提示', type: 'success', duration: 5000 });样式定制
ElementPlus提供了主题切换功能,可以通过修改全局样式来改变整个应用的主题颜色。
/* main.css */ :root { --el-color-primary: #409eff; --el-color-primary-light-7: #b3d8ff; --el-color-primary-light-8: #8dc1ff; --el-color-primary-light-9: #66a3ff; --el-color-primary-light-10: #409eff; --el-color-primary-light-11: #1c6ee2; --el-color-primary-light-12: #0a58c9; --el-color-primary-light-13: #0a43aa; --el-color-primary-light-14: #0a2c6d; --el-color-primary-light-15: #00162b; --el-color-primary-dark-1: #5a8fff; --el-color-primary-dark-2: #66b1ff; --el-color-primary-dark-3: #8ccfff; --el-color-primary-dark-4: #ace0ff; --el-color-primary-dark-5: #c6ecff; --el-color-primary-dark-6: #d4f4ff; --el-color-primary-dark-7: #e6f7ff; }
可以通过在组件上添加自定义的CSS类来修改组件的样式。
<template> <el-button class="custom-button" type="primary">自定义按钮</el-button> </template> <style scoped> .custom-button { background-color: #409eff; border-color: #409eff; } </style>
<template> <el-table :data="tableData" class="custom-table" style="width: 100%"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template> <style scoped> .custom-table { background-color: #fff; border: 1px solid #e6e6e6; border-radius: 4px; } </style>
ElementPlus提供了主题色功能,可以通过设置主题色来改变整个应用的配色方案。
<template> <el-button :class="`${colorType}-button`" type="primary">按钮</el-button> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const colorType = ref('primary'); return { colorType }; } }); </script> <style scoped> .primary-button { background-color: #409eff; } .success-button { background-color: #67c23a; } .info-button { background-color: #909399; } .warning-button { background-color: #e6a23c; } .danger-button { background-color: #f56c6c; } </style>实战案例
创建一个包含用户名和密码输入框以及登录按钮的登录页面。
<template> <div class="login-container"> <el-card class="box-card"> <div slot="header" class="clearfix"> <span>登录页面</span> </div> <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="用户名" prop="username"> <el-input v-model="loginForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input v-model="loginForm.password" type="password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('loginForm')">登录</el-button> </el-form-item> </el-form> </el-card> </div> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const loginForm = ref({ username: '', password: '' }); const rules = ref({ username: [ { required: true, message: '请输入用户名', trigger: 'blur' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' } ] }); const submitForm = (formName) => { const formRef = ref(null); formRef.value.validate((valid) => { if (valid) { alert('提交成功!'); } else { console.log('提交失败!'); return false; } }); }; return { loginForm, rules, submitForm }; } }); </script> <style scoped> .login-container { display: flex; justify-content: center; align-items: center; height: 100vh; } .box-card { width: 480px; } </style>
创建一个页面用于展示从API获取的数据,并使用表格组件进行展示。
<template> <div class="data-container"> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="age" label="年龄" width="180"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> </el-table> </div> </template> <script> import { defineComponent, ref } from 'vue'; import axios from 'axios'; export default defineComponent({ setup() { const tableData = ref([]); axios.get('https://api.example.com/data') .then((response) => { tableData.value = response.data; }) .catch((error) => { console.error('获取数据失败:', error); }); return { tableData }; } }); </script> <style scoped> .data-container { display: flex; justify-content: center; align-items: center; height: 100vh; } </style>
创建一个包含多个输入框和按钮的表单应用,用于收集用户信息。
<template> <div class="form-container"> <el-form :model="formData" ref="formRef" :rules="rules" label-width="100px"> <el-form-item label="姓名" prop="name"> <el-input v-model="formData.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input v-model.number="formData.age"></el-input> </el-form-item> <el-form-item label="性别" prop="gender"> <el-select v-model="formData.gender" placeholder="请选择性别"> <el-option label="男" value="男"></el-option> <el-option label="女" value="女"></el-option> </el-select> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('formRef')">提交</el-button> </el-form-item> </el-form> </div> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const formData = ref({ name: '', age: '', gender: '' }); const rules = ref({ name: [ { required: true, message: '请输入姓名', trigger: 'blur' } ], age: [ { required: true, message: '请输入年龄', trigger: 'blur' }, { type: 'number', message: '年龄必须为数字', trigger: 'blur' } ], gender: [ { required: true, message: '请选择性别', trigger: 'change' } ] }); const submitForm = (formName) => { const formRef = ref(null); formRef.value.validate((valid) => { if (valid) { alert('提交成功!'); } else { console.log('提交失败!'); return false; } }); }; return { formData, rules, submitForm }; } }); </script> <style scoped> .form-container { display: flex; justify-content: center; align-items: center; height: 100vh; } </style>常见问题与解决方案
组件未按预期渲染
样式不生效
组件功能不正常
ElementPlus有多个版本,确保使用的版本与项目需求相符。
Vue 2 vs Vue 3
npm outdated
命令查看项目中使用的ElementPlus版本是否是最新的。官方文档
社区支持
线上教程与课程