一.Element Plus组件库
1. 安装Element Plus
什么是Element Plus?
Element Plus 是基于Vue 3开发的优秀的PC端 开源UI组件库,它是Element的升级版,对于习惯使用Element的人员来说,在学习Element Plus时,不用花费太多的时间。因为Vue 3不再支持IE 11,所以Element Plus也不再支持IE 11及更低的IE版本。
使用npm 或yarn包管理工具安装Element Plus。
![](https://i-blog.csdnimg.cn/direct/f6ffb4cdca344b389a22dd53a521c661.png)
在Vue 3项目中安装Element Plus
步骤一:
![](https://i-blog.csdnimg.cn/direct/80eb28d388054d0c988dff458416aa12.png)
![](https://i-blog.csdnimg.cn/direct/222064b09c394d8388c6613e9872b07d.png)
步骤一:
![](https://i-blog.csdnimg.cn/direct/1b69875c869f4ea6981e5fb71774cf26.png)
![](https://i-blog.csdnimg.cn/direct/8b045250bb584f3d80d59b3fbf0cb33e.png)
步骤二:
![](https://i-blog.csdnimg.cn/direct/651151f8e67f4c009215a21092b6e715.png)
![](https://i-blog.csdnimg.cn/direct/d73ac62f119e4c23ad6a81031b607650.png)
步骤三:
![](https://i-blog.csdnimg.cn/direct/c7011e743c7449e7827fe5ffc9795673.png)
import { createApp } from 'vue'
import './style.css'
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')
步骤四:
执行命令启动服务。
![](https://i-blog.csdnimg.cn/direct/6462ecf9055d4a98aa19c4b04e35a3eb.png)
项目启动后,会默认开启一个本地服务,地址为http://127.0.0.1:5173/。
2. Element Plus中的常用组件
![](https://i-blog.csdnimg.cn/direct/ebac9ac5fd5f4d79a3dc22b037e3d22e.png)
1. Button组件
![](https://i-blog.csdnimg.cn/direct/0f3a9de3c5d04b13a7de95c9dea3f302.png)
![](https://i-blog.csdnimg.cn/direct/df14290330234fd6b64c310b0d277738.png)
![](https://i-blog.csdnimg.cn/direct/c5b9d3da645c4b83bf98bd3e96712f05.png)
![](https://i-blog.csdnimg.cn/direct/49c4f47403d2488d927b7a37f1c0cd55.png)
![](https://i-blog.csdnimg.cn/direct/7506af65441d400f8c81ee068a705769.png)
演示基础的按钮效果
步骤一:创建src\components\Button.vue文件。
<template>
<el-row class="mb-4">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
</el-row>
<!-- 此处省略了两个<el-row></el-row> -->
</template>
步骤二:修改src\main.js文件,切换页面中显示的组件,具体代码如下。
![](https://i-blog.csdnimg.cn/direct/213cf8eccfc5485e877817d9257c5059.png)
在浏览器中查看Element Plus的按钮效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/e9cd3bda4899419a891a1aab6edf3957.png)
演示链接按钮和禁用按钮的使用
步骤二:
创建src\components\Button2.vue文件。
<template>
<el-row class="mb-4">
<el-button link>Round</el-button>
<el-button type="primary" link :disabled="false">Primary</el-button>
<el-button type="success" link :disabled="true">Success</el-button>
<el-button type="info" link>Info</el-button>
<el-button type="warning" link>Warning</el-button>
<el-button type="danger" link>Danger</el-button>
</el-row>
</template>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/0d886527630245ddb3595c2aa026511c.png)
在浏览器中查看Element Plus的链接按钮和禁用按钮效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/e4779277d30e4b9eb226cccfe6a23279.png)
2. Table组件
Element Plus组件库提供了Table组件,用于展示多条结构类似的数据,例如工资表、课程表和计划表等,可以对数据进行排序、筛选、对比或其他自定义操作。
![](https://i-blog.csdnimg.cn/direct/0e848412ae0744399a0c71f28d9b403b.png)
![](https://i-blog.csdnimg.cn/direct/2bbff4f300684afbb03b770a37250855.png)
![](https://i-blog.csdnimg.cn/direct/a3e912544f164b13ad57861209d43d87.png)
演示基础的表格效果
步骤一:
创建src\components\Table.vue文件。
<template>
<el-table :data="tableData" stripe border style="width: 100%">
<el-table-column prop="date" label="日期" width="180" />
<el-table-column prop="name" label="姓名" width="180" />
<el-table-column prop="address" label="住址" width="300" />
</el-table>
</template>
<script setup>
const tableData = [
{ date: '2023-02-03', name: '王五', address: '北京市海淀区' },
// 此处省略了3行数据]
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/50f9db5d08e340d88201e5922eba70cb.png)
在浏览器中查看Element Plus的表格效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/1a10fc59c2a049e4ab911011e2284913.png)
3. Form组件
![](https://i-blog.csdnimg.cn/direct/dc26dfd530fe4cbfa46a24d2d75ad980.png)
![](https://i-blog.csdnimg.cn/direct/eec48413f61c4e589e115334665c9358.png)
![](https://i-blog.csdnimg.cn/direct/8b15d893bc44407a85bdf39fd5758d4e.png)
![](https://i-blog.csdnimg.cn/direct/1a8dc62029de486692bfa2e6a46fa337.png)
![](https://i-blog.csdnimg.cn/direct/2dd3b11076e64953b0b4bf85d5124ddd.png)
演示基础的表单效果
步骤一:
创建src\components\Form.vue文件 ,在Element Plus官方文档中找到Form组件的相关代码,复制部分核心代码到当前文件中。
<template>
<el-form :model="form" label-width="80px" label-position="left">
<el-form-item label="用户名:">
<el-input v-model="form.name" />
</el-form-item>
<!-- 此处省略了两个<el-form-item></el-form-item> -->
</template>
<script setup>
import { reactive } from 'vue'
const form = reactive({ pass:'', name: '' })
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/f1bc61d4e1ca42889745cd37c3a4af27.png)
在浏览器中查看Element Plus的表单效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/7f658b7b96a84a36addc96399102b725.png)
![](https://i-blog.csdnimg.cn/direct/37162d48a14d42798fc170876f69e49f.png)
![](https://i-blog.csdnimg.cn/direct/70614a549acb46a2963e14c0b94868b5.png)
在浏览器中查看表单内容横向排列效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/15d0126c14134c2688fa0e330ed0456b.png)
4. Menu组件
![](https://i-blog.csdnimg.cn/direct/9633180bfaeb45979929ca44d7d288f5.png)
![](https://i-blog.csdnimg.cn/direct/fd1edde64f7d44698858b13d88337e5a.png)
![](https://i-blog.csdnimg.cn/direct/95dafa493d604c2bb9c9c92bbe87014c.png)
![](https://i-blog.csdnimg.cn/direct/f512197b0a8d47088ab428e96dcd069c.png)
演示顶部菜单栏效果
步骤一:
创建src\components\Menu.vue文件。
<template>
<el-menu class="el-menu-demo" mode="horizontal">
<el-menu-item index="1">首页</el-menu-item>
<el-sub-menu index="2"><!-- 此处省略了部分代码 --> </el-sub-menu>
<el-menu-item index="3" disabled>信息</el-menu-item>
<el-menu-item index="4">联系</el-menu-item>
</el-menu>
</template>
<script setup>
import { ref } from 'vue'
const activeIndex = ref('1')
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/74e619ee8be44727a39b8dc42f9e0afe.png)
步骤三:
修改src\main.js文件,将导入style.css的代码进行注释,以免其影响Menu组件的样式效果。
// import './style.css'
在浏览器中查看Element Plus顶部菜单栏效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/f67bac037853462e98b25d33983a6b21.png)
若想实现垂直菜单栏效果,可以修改上述代码,将**<el-menu>标签中class的值改为el-menu-vertical-demo**,将mode的值改为vertical。单击"我的"菜单项,会显示折叠的子菜单信息,如下图所示。
![](https://i-blog.csdnimg.cn/direct/53718aecf2f4474a957f71fc3c7411a1.png)
二.Vant组件库
1. 安装Vant
什么是Vant?
Vant是一个轻量级的、可靠的移动端组件库,于2017年开源。目前Vant官方提供了对Vue 2、Vue 3和微信小程序的支持。
使用npm或yarn包管理工具安装Vant。
![](https://i-blog.csdnimg.cn/direct/a5cb8634f0e2488ab74fa779a7345af7.png)
在Vue 3项目中使用yarn安装Vant
步骤一:
打开命令提示符,切换到D:\vue\chapter06目录,使用yarn创建一个名称为vant-component的项目。
![](https://i-blog.csdnimg.cn/direct/37ada333441c4f6cb8cef2e1f492f127.png)
在命令提示符中,切换到vant-component目录,为项目安装所有依赖。
![](https://i-blog.csdnimg.cn/direct/64067ede94e341568100943d87147944.png)
步骤二:
在vant-component目录下使用yarn安装Vant。
![](https://i-blog.csdnimg.cn/direct/4002d50ca7a94740b5a924014daa8652.png)
步骤三:
使用VS Code编辑器打开vant-component目录,在src\main.js文件中,导入并挂载Vant模块。
import { createApp } from 'vue'
import './style.css'
import Vant from 'vant'
import 'vant/lib/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(Vant)
app.mount('#app')
步骤四:
执行命令启动服务。
yarn dev
项目启动后,会默认开启一个本地服务,地址为http://127.0.0.1:5173/。
2 .Vant中的常用组件
Vant组件库中包含很多组件,由于篇幅有限,仅对Vant组件库中Button组件、Swipe组件、Tab组件、Form组件、Grid组件和Tabbar组件进行讲解。
1. Button组件
Button组件使用<van-button>标签定义,<van-button>标签的常用属性如下表所示。
![](https://i-blog.csdnimg.cn/direct/d13191cf55454b92918baf6666af235f.png)
演示基础的按钮效果
步骤一:
创建src\components\Button.vue文件。
<template>
<van-button type="primary">主要按钮</van-button>
<van-button type="success" round>成功按钮</van-button>
<van-button type="default" disabled>默认按钮</van-button>
<van-button type="warning" block>警告按钮</van-button>
<van-button type="danger" plain hairline>危险按钮</van-button>
<van-button type="danger" loading loading-type="spinner" loading-text="加载中..." />
</template>
<style scoped>
button{ margin: 3px; }
</style>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/4a807fb8f9b24521891cfc655dd1bb36.png)
在浏览器中查看Vant的按钮效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/e1aa6834237a45549258ffd8515e9c0a.png)
演示图标按钮的使用
步骤一:
创建src\components\Button2.vue文件。
<template>
<van-button icon="circle" type="primary">基础图标</van-button>
<van-button icon="like" type="primary">实底风格</van-button>
<van-button icon="phone-o" type="primary">线框风格</van-button>
<van-button :icon="icon">按钮</van-button>
</template>
<script setup>
import icon from '../assets/user-active.png'
</script>
<style>
.van-button { margin: 5px 1px !important; }
</style>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/7bf9cc6291514f54a4b3cceff64dc844.png)
在浏览器中打开开发者工具,测试在移动设备模拟环境下Vant的图标按钮效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/a6ce6d1c65ee4f4abec4570337db718f.png)
![](https://i-blog.csdnimg.cn/direct/91153377f34a4fce9cfb1f2fc361d8f1.png)
演示按钮尺寸和页面导航效果
步骤一:
创建src\components\Button3.vue文件。
<template>
<van-button type="primary" size="large">大型按钮</van-button>
<van-button type="primary" size="normal">普通按钮</van-button>
<van-button type="primary" size="small">小型按钮</van-button>
<van-button type="primary" size="mini">迷你按钮</van-button>
<van-button type="primary" url="/test.html">URL跳转</van-button>
<van-button type="primary" to="目标地址">路由跳转</van-button>
</template>
<style>
.van-button { margin: 5px 1px !important; }
</style>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/1fb5ed29236d4574b37b601d8b1f43ee.png)
在浏览器中查看Vant按钮尺寸和页面导航效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/7e292535ca7f44cfaa80d570acc41454.png)
2. Swipe组件
Vant组件库提供了Swipe组件,用于实现图片轮播效果。轮播图是页面结构中重要的组成部分,常用于网站的首页,主要用于展示页面中的活动信息,让用户不用滚动屏幕就能看到更多内容,可以最大化信息密度。
![](https://i-blog.csdnimg.cn/direct/7653cc1c21894358b4628b6b56706124.png)
![](https://i-blog.csdnimg.cn/direct/771cac9a2f89487ebdac0c6adb21409d.png)
![](https://i-blog.csdnimg.cn/direct/f19c869bf2624e5386fb13b00377a8ae.png)
演示一种简单的图片轮播效果
步骤一:
创建src\components\Swipe.vue文件,在Vant官方文档中找到Swipe组件相关代码,复制部分核心代码到当前文件中。
<template>
<van-swipe :autoplay="3000" lazy-render style="width:300px;">
<van-swipe-item v-for="image in images" :key="image">
<img :src="image" />
</van-swipe-item>
</van-swipe>
</template>
<script setup>
const images = [ '/images/01.jpg', '/images/02.jpg', '/images/03.jpg', '/images/04.jpg']
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/595915c6759d45a69b8d5957278cca02.png)
在浏览器中查看图片的横向滚动如下图所示。
![](https://i-blog.csdnimg.cn/direct/55eaed7070804d28abcffd50aabbca1d.png)
若想要图片纵向滚动,可以为<van-swipe>标签添加vertical属性,并设置滑块容器的高度,使轮播图片纵向排列。在浏览器中查看图片的纵向滚动如下图所示。
![](https://i-blog.csdnimg.cn/direct/578ad51827624c0da0fd7d666341aee6.png)
3. Tab组件
Vant组件库提供了Tab组件,用于实现标签页效果。标签页一般出现在页面的顶部或者页面中,用于在不同的内容区域之间进行切换。
![](https://i-blog.csdnimg.cn/direct/d0148d59a4654f40bb767e5708b8a823.png)
![](https://i-blog.csdnimg.cn/direct/7a61489cf8f04f818cca9a2c5debea8d.png)
![](https://i-blog.csdnimg.cn/direct/8ed6f0e9032345afbacca80339bd92e1.png)
演示一种简单的标签页效果
步骤一:
创建src\components\Tab.vue文件。
<template>
<div style="width: 350x; text-align: center;">
<van-tabs v-model:active="active" swipeable type="card">
<van-tab title="我是标签1的内容">内容 1</van-tab>
<!-- 此处省略3个<van-tab></<van-tab>标签 -->
</div>
</template>
<script setup>
import { ref } from 'vue'
const active = ref(0)
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/a7e42fd7997d4d43a7b81e5fd9f4f004.png)
在浏览器中查看Vant的标签页效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/0c1b956ddfe04d3595efb4012c4f35e4.png)
4. Form组件
Vant组件库提供了Form组件,用于数据输入、校验,支持输入框、单选框、复选框等类型。
![](https://i-blog.csdnimg.cn/direct/31a25b95f0fc43b5b4d3973c4c6fbdc4.png)
Form组件使用**<van-form>标签定义,该标签需要与<van-field>标签搭配使用,用户可以在输入框内输入或编辑文字。<van-field>标签内可以通过rules属性定义校验规则,通过@submit触发**单击事件。
演示一种简单的表单效果
步骤一:
创建src\components\Form.vue文件,在Vant官方文档中找到Form组件相关代码,复制部分核心代码到当前组件中。
<template>
<van-nav-bar title="登录组件" />
<van-form @submit="onSubmit">
<van-cell-group inset>
<van-field v-model="username" :rules="[{ }]" />
<van-field v-model="password" :rules="[{ }]" />
</van-cell-group>
<van-button block type="primary" native-type="submit">提交</van-button>
</van-form>
</template>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/36e43cfb57e144108505106ae01ad41b.png)
步骤三:
修改src\main.js文件,对导入style.css文件的代码进行注释。
// import './style.css'
在浏览器中查看Vant的表单效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/ad067d47261a4cd98172c9cd1c73b49f.png)
单击"提交"按钮后,会进行规则校验,如下图所示。
![](https://i-blog.csdnimg.cn/direct/c58b471aba224a91859858d172d61d00.png)
5. Grid组件
Vant组件库提供了Grid组件,用于实现网格效果,网格可以在水平方向上把页面分隔成等宽度的区块,一般用于同时展示多个同类项目的场景,例如微信支付页面。
![](https://i-blog.csdnimg.cn/direct/7eb2762b64034121a9b1082c918122e8.png)
![](https://i-blog.csdnimg.cn/direct/23b80ea67c614fa182cf3944f88cc1b2.png)
![](https://i-blog.csdnimg.cn/direct/d5844111d9fd464d8a2972fa3dd55f93.png)
演示一种基础的网格效果
步骤一:
创建src\components\Grid.vue文件,在Vant官方文档中找到Grid组件相关代码,复制部分核心代码到当前文件中。
<template>
<van-grid>
<van-grid-item icon="wap-home-o" text="首页" dot />
<van-grid-item icon="chat-o" text="聊天" badge="99+" />
<van-grid-item icon="phone-o" text="电话" />
<van-grid-item icon="user-o" text="我的" />
</van-grid>
<!-- 见下一页 -->
</template>
实现一行中平均显示3列图片,图片素材从配套源代码中获取。
<van-grid :column-num="3" square :gutter="5">
<van-grid-item icon="more-o" text="文字">
<van-image src=" /images/01.jpg" />
</van-grid-item>
<van-grid-item icon="more-o" text="文字">
<van-image src=" /images/02.jpg" />
</van-grid-item>
<van-grid-item icon="more-o" text="文字">
<van-image src=" /images/03.jpg" />
</van-grid-item>
</van-grid>
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/386cd5a7eded4f59b87d0a07b629d918.png)
在浏览器中查看Vant的网格效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/a342057a7ed746bcaac45285d5e905bb.png)
若想网格的内容呈横向排列,则可以为**<van-grid>标签添加direction属性,将属性值设置为horizontal**,网格内容横向排列效果如下图所示。
![](https://i-blog.csdnimg.cn/direct/2253428a0e0044d7b673b03a460f420e.png)
6. Tabbar组件
Vant组件提供了Tabbar组件,用于在不同页面之间进行切换,常放置在页面的底部。
![](https://i-blog.csdnimg.cn/direct/2c080f7c94f241f2b1460f3965813ebd.png)
![](https://i-blog.csdnimg.cn/direct/6f9fbb54d53a4992bbab4a3009897013.png)
![](https://i-blog.csdnimg.cn/direct/a33de57a7c7240f494fc13b865b02f18.png)
演示一种基础的标签栏效果
步骤一:
创建src\components\Tabbar.vue文件,在Vant官方文档中找到Tabbar组件相关代码,复制部分核心代码到当前文件中。
<template>
<van-tabbar v-model="active" fixed active-color="red" inactive-color="blue" :placeholder="true">
<van-tabbar-item icon="home-o">标签</van-tabbar-item>
<!-- 此处省略了3个 <van-tabbar-item>标签 -->
</van-tabbar>
</template>
<script setup>
import { ref } from 'vue'
const active = ref(2)
</script>
步骤二:
修改src\main.js文件,切换页面中显示的组件。
![](https://i-blog.csdnimg.cn/direct/8c375767f9304ea0892a7c627b83acdb.png)
在浏览器中查看Vant的标签栏效果如下图所示。
三.Ant Design Vue组件库
1 .安装Ant Design Vue
什么是Ant Design Vue?
Ant Design Vue是一个优秀的前端UI组件库,由蚂蚁金服体验技术部推出,于2018年3月正式开源,受到众多前端开发者及企业的喜爱。Ant Design Vue基于Vue实现,专门服务于企业级后台产品,支持主流浏览器和服务器端渲染。
使用npm或yarn包管理工具安装Ant Design Vue。
![](https://i-blog.csdnimg.cn/direct/b3a8c67aee074584b3f7c3f6b69e6031.png)
在Vue 3项目中使用yarn安装Ant Design Vue
步骤一:
打开命令提示符,切换到D:\vue\chapter06目录,使用yarn创建一个名称为ant-component的项目。
![](https://i-blog.csdnimg.cn/direct/665073e6c604440ebd89465f577f1547.png)
在命令提示符中,切换到ant-component目录,为项目安装所有依赖。
cd ant-component
yarn
在ant-component目录下使用yarn安装Ant Design Vue。
![](https://i-blog.csdnimg.cn/direct/419feef4c85a4398aa0302d344dc194b.png)
步骤三:
使用VS Code编辑器打开vant-component目录,在src\main.js文件中,导入并挂载Ant Design Vue模块。
import { createApp } from 'vue'
import './style.css'
import AntDesignVue from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import App from './App.vue'
const app = createApp(App)
app.use(AntDesignVue)
app.mount('#app')
步骤四:
![](https://i-blog.csdnimg.cn/direct/3746140928fd44cb98b6d44bdc39d011.png)
2 .Ant Design Vue中的常用组件
1. Button组件
Button组件使用<a-button>标签定义,<a-button>标签的常用属性如下表所示。
![](https://i-blog.csdnimg.cn/direct/fd48af472543444bb16d438de24ac6c4.png)
![](https://i-blog.csdnimg.cn/direct/977b49da10f34997b25e8ffbf531ff9a.png)
![](https://i-blog.csdnimg.cn/direct/e34eaecaf9eb4025b5af3a812e3ef765.png)
<template>
<div :style="{ background: 'rgb(190, 200, 200)', padding: '26px 16px 16px' }">
<a-button type="primary" size="large">主按钮</a-button>
<!-- 此处省略了8个<a-button></a-button>标签 -->
<a-button type="primary">
<template #icon><SearchOutlined /></template>
搜索
</a-button>
</div>
</template>
步骤二:
![](https://i-blog.csdnimg.cn/direct/4c900e105dc845f5994f6ca895913249.png)
![](https://i-blog.csdnimg.cn/direct/483962c3ff36480a89e733189296eafa.png)
2. Layout组件
![](https://i-blog.csdnimg.cn/direct/d80617629f914d219db32b5f98e03bf9.png)
![](https://i-blog.csdnimg.cn/direct/402f972284904a44bcc74e7fc19ccdb9.png)