Vue3 PrimeVue 应用开发实战1
ztj100 2024-11-26 11:15 26 浏览 0 评论
Vue3 PrimeVue 应用开发实战1
1.PrimeVue简介
PrimeVue 是一套非常优秀的 Vue UI 组件库,支持 Vue 3 的 web UI 组件库,组件丰富,定制性很强,官网文档清晰,代码例子充足,而且中文化也做得很不错,是一款可用性很强的 Vue 组件库。
PrimeVue 的技术特性:
- 组件丰富。内置 70 多个常用的组件,体验优秀,使用简单
- 主题丰富。内置大量主题,且提供强大的主题定制工具
- 高效开发模板。由专业设计师设计并由 Vue 专家精心制作的精致的 Vue-CLI 开发模板
- 可访问性强。面向所有人的UI组件,全面支持 WCAG(Web内容可访问性指南)标准
- PrimeVue 是一个与设计无关的库,实现原理是将样式分为 core 和 theme。core 驻留在 PrimeVue 内部,以实现和组件结构相关的效果,例如定位,而 theme 实现颜色,填充和边距。
- PrimeVue 提供各种免费开源的主题和高级主题,我们可以从各种主题中进行选择,也可以使用官方提供 Theme Designer 工具轻松开发自己的主题。
PrimeVue文档:
英文文档:https://primefaces.org/primevue
中文文档:http://www.primevue.top
中文文档和英文文档版本不一致,建议以英文文档为主,参照中文文档,如果英文好的话就只看英文文档。
2.使用PrimeVue
使用PrimeVue,需要使用的主要内容包括:组件、图标、布局系统,所以,需要安装这三个部分,命令如下:
npm install primevue@^3.12.1 --save
npm install primeicons --save
npm install primeflex --save
下一步,设置PrimeVue配置:
import {createApp} from 'vue';
import App from './App.vue';
import PrimeVue from 'primevue/config';
const app = createApp(App);
app.use(PrimeVue);
接下来,导入样式单
primevue/resources/themes/saga-blue/theme.css //theme
primevue/resources/primevue.min.css //core css
primeicons/primeicons.css //icons
如果需要使用布局系统,还需要导入:
primeflex/primeflex.css
main.js文件完整内容:
import { createApp } from 'vue'
import App from './institution.vue'
import PrimeVue from 'primevue/config'
import 'primevue/resources/themes/bootstrap4-light-blue/theme.css' //theme
import 'primevue/resources/primevue.min.css' //core css
import 'primeicons/primeicons.css' //icons
import 'primeflex/primeflex.scss'
const app = createApp(App)
app.use(PrimeVue)
app.mount('#app')
3.主题
PrimeVue提供了一些免费的主题供用户使用,如下:
primevue/resources/themes/bootstrap4-light-blue/theme.css
primevue/resources/themes/bootstrap4-light-purple/theme.css
primevue/resources/themes/bootstrap4-dark-blue/theme.css
primevue/resources/themes/bootstrap4-dark-purple/theme.css
primevue/resources/themes/md-light-indigo/theme.css
primevue/resources/themes/md-light-deeppurple/theme.css
primevue/resources/themes/md-dark-indigo/theme.css
primevue/resources/themes/md-dark-deeppurple/theme.css
primevue/resources/themes/mdc-light-indigo/theme.css
primevue/resources/themes/mdc-light-deeppurple/theme.css
primevue/resources/themes/mdc-dark-indigo/theme.css
primevue/resources/themes/mdc-dark-deeppurple/theme.css
primevue/resources/themes/tailwind-light/theme.css
primevue/resources/themes/fluent-light/theme.css
primevue/resources/themes/lara-light-indigo/theme.css
primevue/resources/themes/lara-dark-indigo/theme.css
primevue/resources/themes/lara-light-purple/theme.css
primevue/resources/themes/lara-dark-purple/theme.css
primevue/resources/themes/lara-light-blue/theme.css
primevue/resources/themes/lara-dark-blue/theme.css
primevue/resources/themes/lara-light-teal/theme.css
primevue/resources/themes/lara-dark-teal/theme.css
primevue/resources/themes/saga-blue/theme.css
primevue/resources/themes/saga-green/theme.css
primevue/resources/themes/saga-orange/theme.css
primevue/resources/themes/saga-purple/theme.css
primevue/resources/themes/vela-blue/theme.css
primevue/resources/themes/vela-green/theme.css
primevue/resources/themes/vela-orange/theme.css
primevue/resources/themes/vela-purple/theme.css
primevue/resources/themes/arya-blue/theme.css
primevue/resources/themes/arya-green/theme.css
primevue/resources/themes/arya-orange/theme.css
primevue/resources/themes/arya-purple/theme.css
primevue/resources/themes/nova/theme.css
primevue/resources/themes/nova-alt/theme.css
primevue/resources/themes/nova-accent/theme.css
primevue/resources/themes/nova-vue/theme.css
primevue/resources/themes/luna-amber/theme.css
primevue/resources/themes/luna-blue/theme.css
primevue/resources/themes/luna-green/theme.css
primevue/resources/themes/luna-pink/theme.css
primevue/resources/themes/rhea/theme.css
我们可以根据自己的需要直接导入使用。
4.使用组件
按需导入:需要什么组件导入什么组件,可以在main.js文件中导入后全局注册,也可以在需要使用的组件中导入后局部注册。
如,局部注册如下:
<template>
......
<Dialog></Dialog>
......
</template>
<script>
import Dialog from 'primevue/dialog'
......
export default {
......
components: {
Dialog
}
......
}
</script>
5.开发实战
实战案例:实现一个子页面框架。
所谓子页面是指在单页面开发中路由显示的页面,即<router-view/>中显示的内容,或者是多页面开发中在<div></div>或<iframe/>中显示的页面。为了统一风格,我们在实际开发中需要对子页面进行一些规范,比如划分区域,视窗大小等。
本节案例的子页面按照后台管理系统的要求进行设计,即:
子页面区域包括:
- 外边框 - 指页面外边框。
- 标题栏 - 左侧显示标题,个别情况下右侧显示操作按钮或者提示信息等。
- 查询栏 - 用于查询数据设置条件,及页面包含的操作,如增删改等。
- 数据栏 - 用于显示数据,一般是表格。
- 表单对话框 - 用于在数据增加、修改时填写表单。
整个子页面设计中,可以固定高度的只有标题栏,其他都有可能随着操作数据的不同而不同,所以外边框应该是100%充满显示区域,查询栏根据查询内容的多少及操作按钮的多少动态调整,剩余的空间都给数据栏,所以数据栏需要滚动条,如果数据栏中是表格的话,数据在表格中上下滚动。
根据以上需求,各部分设计及样式单分别如下:
5.1 外边框
<div style="height: 100vh; padding: 6px;">
</div>
5.2标题栏
标题栏直接使用PrimeVue的Toolbar即可,代码如下:
<Toolbar style="padding: 12px;">
<template #start>
<i class="pi pi-bars p-toolbar-separator mr-2" />
<Tag value="标题"></Tag>
</template>
<template #end>
<Button icon="pi pi-search" class="mr-2" />
<Button icon="pi pi-calendar" class="p-button-success mr-2" />
<Button icon="pi pi-times" class="p-button-danger" />
</template>
</Toolbar>
5.3查询栏
<div class="query-box">
<div class="grid">
<div class="col-3">
<div class="query-field-title">查询字段</div>
<InputText type="text"/>
</div>
<div class="col-9" style="padding-top: 15px;">
<Button type="button" label="查询" icon="pi pi-search" style="margin-right: 12px;"/>
<Button type="button" label="全部" icon="pi pi-search" style="margin-right: 12px;"/>
<Button type="button" label="新建" icon="pi pi-plus" style="margin-right: 12px;"/>
<Button type="button" label="编辑" icon="pi pi-pencil" style="margin-right: 12px;"/>
<Button type="button" label="删除" icon="pi pi-times" style="margin-right: 12px;"/>
</div>
</div>
</div>
查询栏内部使用了PrimeFlex的Grid布局,此Grid布局总宽度为12列,当大于12列后会自动换行,根据实际需要进行调整即可,比如本例只用了一行,左侧为查询字段,右侧为操作按钮,所以class分别是:col-3和col-9,在查询字段的单元格中,内容为:
<div class="query-field-title">查询字段</div>
<InputText type="text" v-model="kw"/>
显示效果如下:
查询栏外边框的样式类 query-box,代码如下:
.query-box {
margin-left: 1px;
margin-right: 1px;
margin-top: 12px;
background: #ffffff;
border-radius: 6px;
padding-top: 12px;
padding-left: 12px;
padding-right: 12px;
padding-bottom: 0px;
box-shadow: #999999 0px 0px 6px;
}
查询字段标题的样式类 query-field-title,代码如下:
.query-field-title {
font-size: 10px;
padding-bottom: 6px;
color: #999999;
}
5.4数据栏
数据栏中一般是表格,所以在此,我们只设计一个外边框,代码如下:
<div class="table-box" style="height: calc(100% - 156px);">
</div>
这里,动态高度通过css来设置,代码:
height: calc(100% - 156px);
156px需要在实际开发中手工计算。
外边框样式类 table-box 代码:
.table-box {
margin-left: 1px;
margin-right: 1px;
margin-top: 12px;
background: #ffffff;
border-radius: 6px;
padding-top: 12px;
padding-left: 12px;
padding-right: 12px;
padding-bottom: 0px;
box-shadow: #999999 0px 0px 6px;
}
5.5表单对话框
表单对话框直接用PrimeVue的Dialog组件来实现,代码如下:
<Dialog v-model:visible="dialogVisible" :modal="true" :draggable="true" style="width: 600px;">
<template #header>
<Tag value="表单标题" icon="pi pi-list"></Tag>
</template>
<div class="form-box">
<div class="grid">
<div class="col-2 form-field-title">表单字段</div>
<div class="col-10">
......
</div>
<div class="col-2 form-field-title">表单字段</div>
<div class="col-10">
......
</div>
......
</div>
</div>
<template #footer>
<Button label="取消" icon="pi pi-times" class="p-button-text" />
<Button label="确定" icon="pi pi-check" autofocus />
</template>
</Dialog>
在对话框中,使用了 PrimeFlex的 Grid 来布局字段与输入组件,这样做,比较灵活可控,可以根据自己的需要进行编排字段和控件的布局,比如使用单列式,多列式等。
对话框中 Grid 外边框样式类 form-box 代码:
.form-box {
border: 1px solid #E9EDF1;
background: #f8f9fa;
padding-top: 8px;
padding-left: 12px;
padding-right: 12px;
border-radius: 6px;
box-shadow: #999999 0px 0px 6px;
}
对话框中表单字段标题样式类 form-field-title 代码:
.form-field-title {
color: #999999;
}
6.整体代码及效果
示例代码如下:
<template>
<!--外边框-->
<div style="height: 100vh; padding: 6px;">
<!--标题栏-->
<Toolbar style="padding: 12px;">
<template #start>
<i class="pi pi-bars p-toolbar-separator mr-2" />
<Tag value="标题"></Tag>
</template>
<template #end>
<Button icon="pi pi-search" class="p-button-sm" style="margin-right: 6px;"/>
<Button icon="pi pi-calendar" class="p-button-success p-button-sm" style="margin-right: 6px;"/>
<Button icon="pi pi-times" class="p-button-danger p-button-sm" />
</template>
</Toolbar>
<!--查询栏-->
<div class="query-box">
<div class="grid">
<div class="col-3">
<div class="query-field-title">查询字段</div>
<InputText type="text"/>
</div>
<div class="col-9" style="padding-top: 15px;">
<Button type="button" label="查询" icon="pi pi-search" style="margin-right: 12px;"/>
<Button type="button" label="全部" icon="pi pi-search" style="margin-right: 12px;"/>
<Button type="button" label="新建" icon="pi pi-plus" style="margin-right: 12px;" @click="dialogVisible = true"/>
<Button type="button" label="编辑" icon="pi pi-pencil" style="margin-right: 12px;"/>
<Button type="button" label="删除" icon="pi pi-times" style="margin-right: 12px;"/>
</div>
</div>
</div>
<!--数据栏-->
<div class="table-box" style="height: calc(100% - 156px);">
</div>
</div>
<!--表单对话框-->
<Dialog v-model:visible="dialogVisible" :modal="true" :draggable="true" style="width: 600px;">
<template #header>
<Tag value="表单标题" icon="pi pi-list"></Tag>
</template>
<div class="form-box">
<div class="grid">
<div class="col-2 form-field-title">表单字段1</div>
<div class="col-10">
<InputText type="text"/>
</div>
<div class="col-2 form-field-title">表单字段2</div>
<div class="col-10">
<InputText type="text"/>
</div>
</div>
</div>
<template #footer>
<Button label="取消" icon="pi pi-times" class="p-button-text" @click="dialogVisible = false"/>
<Button label="确定" icon="pi pi-check" autofocus />
</template>
</Dialog>
</template>
<script>
import Toolbar from 'primevue/toolbar'
import Tag from 'primevue/tag'
import Button from 'primevue/button'
import InputText from 'primevue/inputtext'
import Dialog from 'primevue/dialog'
export default {
name: "UserManage",
components: {
Toolbar,
Tag,
Button,
InputText,
Dialog
},
data () {
return {
dialogVisible: false
}
}
}
</script>
<style scoped>
@import "../../assets/css/sub_frame.css";
</style>
页面效果:
表单效果:
相关推荐
- 再说圆的面积-蒙特卡洛(蒙特卡洛方法求圆周率的matlab程序)
-
在微积分-圆的面积和周长(1)介绍微积分方法求解圆的面积,本文使用蒙特卡洛方法求解圆面积。...
- python创建分类器小结(pytorch分类数据集创建)
-
简介:分类是指利用数据的特性将其分成若干类型的过程。监督学习分类器就是用带标记的训练数据建立一个模型,然后对未知数据进行分类。...
- matplotlib——绘制散点图(matplotlib散点图颜色和图例)
-
绘制散点图不同条件(维度)之间的内在关联关系观察数据的离散聚合程度...
- python实现实时绘制数据(python如何绘制)
-
方法一importmatplotlib.pyplotaspltimportnumpyasnpimporttimefrommathimport*plt.ion()#...
- 简单学Python——matplotlib库3——绘制散点图
-
前面我们学习了用matplotlib绘制折线图,今天我们学习绘制散点图。其实简单的散点图与折线图的语法基本相同,只是作图函数由plot()变成了scatter()。下面就绘制一个散点图:import...
- 数据分析-相关性分析可视化(相关性分析数据处理)
-
前面介绍了相关性分析的原理、流程和常用的皮尔逊相关系数和斯皮尔曼相关系数,具体可以参考...
- 免费Python机器学习课程一:线性回归算法
-
学习线性回归的概念并从头开始在python中开发完整的线性回归算法最基本的机器学习算法必须是具有单个变量的线性回归算法。如今,可用的高级机器学习算法,库和技术如此之多,以至于线性回归似乎并不重要。但是...
- 用Python进行机器学习(2)之逻辑回归
-
前面介绍了线性回归,本次介绍的是逻辑回归。逻辑回归虽然名字里面带有“回归”两个字,但是它是一种分类算法,通常用于解决二分类问题,比如某个邮件是否是广告邮件,比如某个评价是否为正向的评价。逻辑回归也可以...
- 【Python机器学习系列】拟合和回归傻傻分不清?一文带你彻底搞懂
-
一、拟合和回归的区别拟合...
- 推荐2个十分好用的pandas数据探索分析神器
-
作者:俊欣来源:关于数据分析与可视化...
- 向量数据库:解锁大模型记忆的关键!选型指南+实战案例全解析
-
本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在...
- 用Python进行机器学习(11)-主成分分析PCA
-
我们在机器学习中有时候需要处理很多个参数,但是这些参数有时候彼此之间是有着各种关系的,这个时候我们就会想:是否可以找到一种方式来降低参数的个数呢?这就是今天我们要介绍的主成分分析,英文是Princip...
- 神经网络基础深度解析:从感知机到反向传播
-
本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在...
- Python实现基于机器学习的RFM模型
-
CDA数据分析师出品作者:CDALevelⅠ持证人岗位:数据分析师行业:大数据...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)