当前位置: 代码网 > it编程>前端脚本>Vue.js > 在Vue3中使用vue3-print-nb实现前端打印功能

在Vue3中使用vue3-print-nb实现前端打印功能

2024年07月02日 Vue.js 我要评论
前言在前端开发中,经常需要打印页面的特定部分,比如客户列表或商品详情页。要快速实现这些功能,可以使用 vue3-print-nb 插件。它通过对 dom 元素的操作和 css 样式的处理,轻松实现页面

前言

在前端开发中,经常需要打印页面的特定部分,比如客户列表或商品详情页。要快速实现这些功能,可以使用 vue3-print-nb 插件。它通过对 dom 元素的操作和 css 样式的处理,轻松实现页面内容的打印功能。

安装

当前示例以vue3+elementplus为例,如果要使用vue2版本的就安装npm install vue-print-nb --save

npm install vue3-print-nb --save
import { createapp } from 'vue'
import elementplus from 'element-plus'
import 'element-plus/dist/index.css'
import print from 'vue3-print-nb'
import './style.css'
import app from './app.vue'
const app = createapp(app)

app.use(elementplus)
app.use(print)
app.mount('#app')

使用

<script setup>
import { ref } from 'vue'

defineprops({
  msg: string,
})

const count = ref(0)

const tabledata = [
  {
    date: '2016-05-03',
    name: 'tom',
    address: 'no. 189, grove st, los angeles',
  },
  {
    date: '2016-05-02',
    name: 'tom',
    address: 'no. 189, grove st, los angeles',
  },
  {
    date: '2016-05-04',
    name: 'tom',
    address: 'no. 189, grove st, los angeles',
  },
  {
    date: '2016-05-01',
    name: 'tom',
    address: 'no. 189, grove st, los angeles',
  },
]

</script>

<template>
  <h1>{{ msg }}</h1>
  <div class="btn">
    <el-button type="primary" v-print="'#printtable'">打印</el-button>
  </div>
  <el-table id="printtable" :data="tabledata" border style="width: 100%">
    <el-table-column prop="date" label="date" width="180" />
    <el-table-column prop="name" label="name" width="180" />
    <el-table-column prop="address" label="address" />
  </el-table>
</template>

只需要在要打印的元素上通过v-print属性即可实现打印的效果,可以选择打印全部或者打印指定页面,比如我只想要打印el-table表格部分,只需要在el-button按钮上面绑定v-print="'#printtable'",我已经提前在el-table上定义好了id="printtable"v-print的属性值对应的就是el-table。 打印效果预览

以上实现了一个最基本的打印效果,v-print还支持更多的属性呢!它的属性值可以是一个对象以此来实现更加定制化的打印效果,一起来看看吧

html

  <div class="btn">
    <el-button type="primary" v-print="printobj">打印</el-button>
  </div>
  <el-table id="printtable" :data="tabledata" border style="width: 100%">
    <el-table-column prop="date" label="date" width="180" />
    <el-table-column prop="name" label="name" width="180" />
    <el-table-column prop="address" label="address" />
  </el-table>

javascript

const printobj = {
  id: 'printtable',
  poptitle: 'print nb',
  extracss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extrahead: '<meta http-equiv="content-language"content="zh-cn"/>',
  beforeopencallback (vue) {
    console.log('打开之前')
  },
  opencallback (vue) {
    console.log('执行了打印')
  },
  closecallback (vue) {
    console.log('关闭了打印工具')
  }
}

我们可以给要打印的页面指定额外的样式、额外的样式、额外头,甚至是添加回调函数!

打印网址

printobj对象添加一个url属性即可实现打印当前网址对应的整个页面。但是如何设置了url数据就不能再同时设置id属性了。还有一点需要的注意的是设置url属性需要确保同源策略相同!

const printobj = {
  url: 'http://localhost:5173/',
  poptitle: 'good print',
  extracss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extrahead: '<meta http-equiv="content-language"content="zh-cn"/>',
  beforeopencallback (vue) {
    console.log('打开之前')
  },
  opencallback (vue) {
    console.log('执行了打印')
  },
  closecallback (vue) {
    console.log('关闭了打印工具')
  }
}

打印预览

设置了preview属性将在打印时候显示打印预览。

const printobj = {
  id: 'printtable',
  preview:true, // 打印预览
  previewtitle: '打印预览',
  poptitle: 'good print',
  extracss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extrahead: '<meta http-equiv="content-language"content="zh-cn"/>',
  beforeopencallback (vue) {
    console.log('打开之前')
  },
  opencallback (vue) {
    console.log('执行了打印')
  },
  closecallback (vue) {
    console.log('关闭了打印工具')
  }
}

打印预览效果

v-print api

参数说明类型可选项默认值
id范围打印id,必填值string
standard文档类型(仅打印本地范围)stringhtml5/loose/stricthtml5
extrahead在节点中添加 dom 节点,多个节点用 分隔,(仅打印局部范围)string
extracss新的 css 样式表,并使用 分隔多个节点,(仅打印局部范围)string-
poptitle标签内容(仅打印本地范围)string-
opencallback调用打印工具成功回调函数functionreturns the instance of vue called at that time-
closecallback关闭打印工具成功回调函数functionreturns the instance of vue called at that time-
beforeopencallback调用打印工具前的回调函数functionreturns the instance of vue called at that time-
url打印指定url。(不可同时设置id)string--
asyncurl通过 'resolve()' 返回 urlfunction--
preview预览工具boolean-false
previewtitle预览工具标题string-'打印预览'
previewprintbtnlabel预览工具按钮的名称string-'打印'
zindex预览工具的 css:z-indexstring,number-20002
previewbeforeopencallback启动预览工具前的回调函数functionreturns the instance of vue-
previewopencallback预览工具完全打开后的回调函数functionreturns the instance of vue-

官方文档:https://github.com/power-kxlee/vue3-print-nb?tab=readme-ov-file#v-print-api

到此这篇关于在vue3中使用vue3-print-nb实现前端打印功能的文章就介绍到这了,更多相关vue3前端打印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com