个人主页:Guiat
归属专栏:Vue
正文
Vue生态系统中有多个优秀的UI组件库,帮助开发者快速构建美观、一致且功能丰富的用户界面。以下是三个最受欢迎的框架:
这些框架的市场份额如下:
// Vue UI框架流行度数据模拟
const frameworkPopularity = {
"Element Plus": 38,
"Ant Design Vue": 32,
"Vant": 22,
"其他": 8
};
选择合适的UI框架需要考虑以下几个关键因素:
Element Plus是基于Vue 3的组件库,提供了丰富的桌面端UI组件。
# 使用npm安装
npm install element-plus
# 使用yarn安装
yarn add element-plus
完整引入方式:
// main.js
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
按需引入方式:
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import { ElButton, ElSelect } from 'element-plus'
import 'element-plus/es/components/button/style/css'
import 'element-plus/es/components/select/style/css'
const app = createApp(App)
app.component(ElButton.name, ElButton)
app.component(ElSelect.name, ElSelect)
app.mount('#app')
默认按钮
主要按钮
成功按钮
危险按钮
Element Plus支持通过CSS变量快速定制主题:
/* 在:root作用域下定义CSS变量 */
:root {
--el-color-primary: #6b48ff;
--el-color-success: #67c23a;
--el-color-warning: #e6a23c;
--el-color-danger: #f56c6c;
--el-color-info: #909399;
--el-font-size-base: 14px;
}
也可以通过SCSS变量进行深度定制:
// styles/element-variables.scss
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
$colors: (
'primary': (
'base': #6b48ff,
),
),
);
// 导入Element Plus样式
@use "element-plus/theme-chalk/src/index.scss" as *;
优点:
缺点:
Ant Design Vue是蚂蚁金服设计规范的Vue实现,提供了企业级UI设计语言和高质量组件。
# 使用npm安装
npm install ant-design-vue@next
# 使用yarn安装
yarn add ant-design-vue@next
完整引入:
// main.js
import { createApp } from 'vue'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import App from './App.vue'
const app = createApp(App)
app.use(Antd)
app.mount('#app')
按需引入:
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import { Button, Select } from 'ant-design-vue'
import 'ant-design-vue/es/button/style/css'
import 'ant-design-vue/es/select/style/css'
const app = createApp(App)
app.component(Button.name, Button)
app.component(Select.name, Select)
app.mount('#app')
默认按钮
主要按钮
虚线按钮
危险按钮
技术部
市场部
Ant Design Vue支持通过Less变量进行主题定制:
// vue.config.js
module.exports = {
css: {
loaderOptions: {
less: {
lessOptions: {
modifyVars: {
'primary-color': '#1DA57A',
'link-color': '#1DA57A',
'border-radius-base': '2px',
},
javascriptEnabled: true,
},
},
},
},
};
优点:
缺点:
Vant是有赞前端团队推出的移动端UI组件库,专为移动应用场景设计。
# 使用npm安装
npm i vant
# 使用yarn安装
yarn add vant
完整引入:
// main.js
import { createApp } from 'vue';
import Vant from 'vant';
import 'vant/lib/index.css';
import App from './App.vue';
const app = createApp(App);
app.use(Vant);
app.mount('#app');
按需引入(推荐,配合unplugin-vue-components自动导入):
// vite.config.js
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
export default {
plugins: [
vue(),
Components({
resolvers: [VantResolver()],
}),
],
};
默认按钮
主要按钮
成功按钮
危险按钮
提交
Vant提供了CSS变量方式进行主题定制:
:root {
--van-primary-color: #1989fa;
--van-success-color: #07c160;
--van-danger-color: #ee0a24;
--van-warning-color: #ff976a;
--van-text-color: #323233;
--van-border-radius: 4px;
}
优点:
缺点:
不同框架的包体积大小(完整引入版本):
// 包体积大小(KB)
const packageSizes = {
"Element Plus": 2580,
"Ant Design Vue": 3150,
"Vant": 1620
};
基于1000个组件同时渲染的场景下的平均渲染时间(毫秒):
// 渲染性能(毫秒,越低越好)
const renderPerformance = {
"Element Plus": 380,
"Ant Design Vue": 420,
"Vant": 210
};
三个框架都支持组件按需加载,可有效减少生产环境的包体积:
// 按需加载后的包体积减少百分比
const lazyLoadImprovement = {
"Element Plus": "65%",
"Ant Design Vue": "70%",
"Vant": "60%"
};
在某些特殊场景下,可能需要混合使用多个UI框架。例如在一个同时包含管理后台和H5页面的项目中:
// main.js - 桌面端入口
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
// mobile.js - 移动端入口
import { createApp } from 'vue'
import Vant from 'vant'
import 'vant/lib/index.css'
import MobileApp from './MobileApp.vue'
const mobileApp = createApp(MobileApp)
mobileApp.use(Vant)
mobileApp.mount('#mobile-app')
但混合使用会造成包体积增大,建议使用动态导入和路由懒加载优化:
// router.js
const Desktop = () => import('./views/Desktop.vue')
const Mobile = () => import('./views/Mobile.vue')
const routes = [
{
path: '/admin',
component: Desktop,
// 动态加载Element Plus
beforeEnter: (to, from, next) => {
import('element-plus').then(module => {
app.use(module.default)
next()
})
}
},
{
path: '/mobile',
component: Mobile,
// 动态加载Vant
beforeEnter: (to, from, next) => {
import('vant').then(module => {
app.use(module.default)
next()
})
}
}
]
管理系统
首页
用户管理
产品管理
导航一
选项1
选项2
导航二
编辑
删除
加入购物车
立即购买
首页
分类
购物车
我的
基于项目需求选择:
考虑团队熟悉度:
结语
感谢您的阅读!期待您的一键三连!欢迎指正!