1. 首页
  2. 技术知识

Vue3 PrimeVue 应用开发实战1

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 –savenpm install primeicons –savenpm 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       //themeprimevue/resources/primevue.min.css                 //core cssprimeicons/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’       //themeimport ‘primevue/resources/primevue.min.css’                 //core cssimport ‘primeicons/primeicons.css’                          //iconsimport ‘primeflex/primeflex.scss’const app = createApp(App)app.use(PrimeVue)app.mount(‘#app’)
3.主题

PrimeVue提供了一些免费的主题供用户使用,如下:

primevue/resources/themes/bootstrap4-light-blue/theme.cssprimevue/resources/themes/bootstrap4-light-purple/theme.cssprimevue/resources/themes/bootstrap4-dark-blue/theme.cssprimevue/resources/themes/bootstrap4-dark-purple/theme.cssprimevue/resources/themes/md-light-indigo/theme.cssprimevue/resources/themes/md-light-deeppurple/theme.cssprimevue/resources/themes/md-dark-indigo/theme.cssprimevue/resources/themes/md-dark-deeppurple/theme.cssprimevue/resources/themes/mdc-light-indigo/theme.cssprimevue/resources/themes/mdc-light-deeppurple/theme.cssprimevue/resources/themes/mdc-dark-indigo/theme.cssprimevue/resources/themes/mdc-dark-deeppurple/theme.cssprimevue/resources/themes/tailwind-light/theme.cssprimevue/resources/themes/fluent-light/theme.cssprimevue/resources/themes/lara-light-indigo/theme.cssprimevue/resources/themes/lara-dark-indigo/theme.cssprimevue/resources/themes/lara-light-purple/theme.cssprimevue/resources/themes/lara-dark-purple/theme.cssprimevue/resources/themes/lara-light-blue/theme.cssprimevue/resources/themes/lara-dark-blue/theme.cssprimevue/resources/themes/lara-light-teal/theme.cssprimevue/resources/themes/lara-dark-teal/theme.cssprimevue/resources/themes/saga-blue/theme.cssprimevue/resources/themes/saga-green/theme.cssprimevue/resources/themes/saga-orange/theme.cssprimevue/resources/themes/saga-purple/theme.cssprimevue/resources/themes/vela-blue/theme.cssprimevue/resources/themes/vela-green/theme.cssprimevue/resources/themes/vela-orange/theme.cssprimevue/resources/themes/vela-purple/theme.cssprimevue/resources/themes/arya-blue/theme.cssprimevue/resources/themes/arya-green/theme.cssprimevue/resources/themes/arya-orange/theme.cssprimevue/resources/themes/arya-purple/theme.cssprimevue/resources/themes/nova/theme.cssprimevue/resources/themes/nova-alt/theme.cssprimevue/resources/themes/nova-accent/theme.cssprimevue/resources/themes/nova-vue/theme.cssprimevue/resources/themes/luna-amber/theme.cssprimevue/resources/themes/luna-blue/theme.cssprimevue/resources/themes/luna-green/theme.cssprimevue/resources/themes/luna-pink/theme.cssprimevue/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>
页面效果:


表单效果:

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/126877.html

联系我们