Vue3 PrimeVue 应用开发实战1
ztj100 2024-11-26 11:15 30 浏览 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>
页面效果:
表单效果:
相关推荐
- Java对象序列化与反序列化的那些事
-
Java对象序列化与反序列化的那些事在Java的世界里,对象序列化和反序列化就像一对孪生兄弟,它们共同构成了Java对象存储和传输的基础。如果你曾经尝试将对象保存到文件中,或者在网络中传输对象,那么你...
- 集合或数组转成String字符串(集合怎么转换成字符串)
-
1.将集合转成String字符串Strings="";for(inti=0;i<numList.size;i++){if(s==""){s=numL...
- java学习分享:Java截取(提取)子字符串(substring())
-
在String中提供了两个截取字符串的方法,一个是从指定位置截取到字符串结尾,另一个是截取指定范围的内容。下面对这两种方法分别进行介绍。1.substring(intbeginIndex)形...
- deepseek提示词:sql转c#代码示例。
-
SELECTRIGHT('0000'+CAST(DATEDIFF(DAY,'2024-01-01',GETDATE())ASVARCHAR(4)),4)...
- Java 21 新特性的实践,确实很丝滑!
-
1虚拟线程创建虚拟线程...
- 为什么Java中的String是不可变的(Immutable)
-
在Java中,String类型是用于表示字符串的类,而字符串则是字符序列,是Java编程中最常用的数据类型之一。String类是不可变的,这意味着一旦创建,字符串的值就不能改变,下面我们就来介绍一下为...
- Java中读取File文件内容转为String类型
-
@Java讲坛杨工开发中常常会碰到读取磁盘上的配置文件等内容,然后获取文件内容转字符串String类型,那么就需要编写一个API来实现这样的功能。首先准备一个测试需要的文件test.xml...
- 从Pandas快速切换到Polars :数据的ETL和查询
-
对于我们日常的数据清理、预处理和分析方面的大多数任务,Pandas已经绰绰有余。但是当数据量变得非常大时,它的性能开始下降。我们以前的两篇文章来测试Pandas1.5.3、polar和Pandas...
- Pandas高手养成记:10个鲜为人知的高效数据处理技巧
-
Pandas是Python中非常强大的数据分析库,提供了高效的数据结构和数据处理工具。以下是一些鲜为人知但极其有用的Pandas数据处理技巧,可以帮助你提高工作效率:使用.eval()执行行...
- 灵活筛选数据,pandas无需指定行列的筛选方法,步骤详解
-
pandas库可轻松地筛选出符合特定条件的数据,无需指定筛选的行和列。通过灵活运用pandas的筛选功能,我们能够高效、准确地获取到感兴趣的数据,本文将介绍以下几种方法,在不指定行列的情况下使用pan...
- 【Pandas】(4)基本操作(pandas的基本操作)
-
选择数据获取列单列获取要获取DataFrame的单个列,你可以使用列名以两种不同的方式:...
- 「Python数据分析」Pandas基础,用iloc函数按行列位置选择数据
-
前面我们学过,使用loc函数,通过数据标签,也就是行标签和列标签来选择数据。行和列的标签,是在数据获取,或者是生成的时候,就已经定义好的。行数据标签,也就是唯一标识数据,不重复的一列,相当于数据库中的...
- Python数据的选取和处理(python数据提取方法)
-
importpandasaspdimportnumpyasnpdata=pd.DataFrame(np.arange(1,10).reshape(3,3),index=['...
- 天秀!一张图就能彻底搞定Pandas(10分钟搞定pandas)
-
作者:刘早起公众号:早起Python大家好,在三月初,我曾给大家分享过一份Matplotlib绘图小抄,详见收下这份来自GitHub的神器,一图搞定Matplotlib!昨天在面向GitHub编程时,...
- Python学不会来打我(92)python代码调试知识总结(五)属性问题
-
Attributeerror是属性问题,这个问题的报错也经常会出现,今天我们就分享一下:Python中引发AttributeError的常见原因及对应解决方案的详细分析。...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)