百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Vue3 PrimeVue 应用开发实战1

ztj100 2024-11-26 11:15 22 浏览 0 评论

Vue3 PrimeVue 应用开发实战1

1.PrimeVue简介

PrimeVue 是一套非常优秀的 Vue UI 组件库,支持 Vue 3 的 web UI 组件库,组件丰富,定制性很强,官网文档清晰,代码例子充足,而且中文化也做得很不错,是一款可用性很强的 Vue 组件库。

PrimeVue 的技术特性:

  1. 组件丰富。内置 70 多个常用的组件,体验优秀,使用简单
  2. 主题丰富。内置大量主题,且提供强大的主题定制工具
  3. 高效开发模板。由专业设计师设计并由 Vue 专家精心制作的精致的 Vue-CLI 开发模板
  4. 可访问性强。面向所有人的UI组件,全面支持 WCAG(Web内容可访问性指南)标准
  5. PrimeVue 是一个与设计无关的库,实现原理是将样式分为 core 和 theme。core 驻留在 PrimeVue 内部,以实现和组件结构相关的效果,例如定位,而 theme 实现颜色,填充和边距。
  6. 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>

页面效果:

表单效果:




相关推荐

如何将数据仓库迁移到阿里云 AnalyticDB for PostgreSQL

阿里云AnalyticDBforPostgreSQL(以下简称ADBPG,即原HybridDBforPostgreSQL)为基于PostgreSQL内核的MPP架构的实时数据仓库服务,可以...

Python数据分析:探索性分析

写在前面如果你忘记了前面的文章,可以看看加深印象:Python数据处理...

CSP-J/S冲奖第21天:插入排序

...

C++基础语法梳理:算法丨十大排序算法(二)

本期是C++基础语法分享的第十六节,今天给大家来梳理一下十大排序算法后五个!归并排序...

C 语言的标准库有哪些

C语言的标准库并不是一个单一的实体,而是由一系列头文件(headerfiles)组成的集合。每个头文件声明了一组相关的函数、宏、类型和常量。程序员通过在代码中使用#include<...

[深度学习] ncnn安装和调用基础教程

1介绍ncnn是腾讯开发的一个为手机端极致优化的高性能神经网络前向计算框架,无第三方依赖,跨平台,但是通常都需要protobuf和opencv。ncnn目前已在腾讯多款应用中使用,如QQ,Qzon...

用rust实现经典的冒泡排序和快速排序

1.假设待排序数组如下letmutarr=[5,3,8,4,2,7,1];...

ncnn+PPYOLOv2首次结合!全网最详细代码解读来了

编辑:好困LRS【新智元导读】今天给大家安利一个宝藏仓库miemiedetection,该仓库集合了PPYOLO、PPYOLOv2、PPYOLOE三个算法pytorch实现三合一,其中的PPYOL...

C++特性使用建议

1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...

Qt4/5升级到Qt6吐血经验总结V202308

00:直观总结增加了很多轮子,同时原有模块拆分的也更细致,估计为了方便拓展个管理。把一些过度封装的东西移除了(比如同样的功能有多个函数),保证了只有一个函数执行该功能。把一些Qt5中兼容Qt4的方法废...

到底什么是C++11新特性,请看下文

C++11是一个比较大的更新,引入了很多新特性,以下是对这些特性的详细解释,帮助您快速理解C++11的内容1.自动类型推导(auto和decltype)...

掌握C++11这些特性,代码简洁性、安全性和性能轻松跃升!

C++11(又称C++0x)是C++编程语言的一次重大更新,引入了许多新特性,显著提升了代码简洁性、安全性和性能。以下是主要特性的分类介绍及示例:一、核心语言特性1.自动类型推导(auto)编译器自...

经典算法——凸包算法

凸包算法(ConvexHull)一、概念与问题描述凸包是指在平面上给定一组点,找到包含这些点的最小面积或最小周长的凸多边形。这个多边形没有任何内凹部分,即从一个多边形内的任意一点画一条线到多边形边界...

一起学习c++11——c++11中的新增的容器

c++11新增的容器1:array当时的初衷是希望提供一个在栈上分配的,定长数组,而且可以使用stl中的模板算法。array的用法如下:#include<string>#includ...

C++ 编程中的一些最佳实践

1.遵循代码简洁原则尽量避免冗余代码,通过模块化设计、清晰的命名和良好的结构,让代码更易于阅读和维护...

取消回复欢迎 发表评论: