前言
在前端开发中,经常需要打印页面的特定部分,比如客户列表或商品详情页。要快速实现这些功能,可以使用 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 | 文档类型(仅打印本地范围) | string | html5/loose/strict | html5 |
extrahead | 在节点中添加 dom 节点,多个节点用 分隔,(仅打印局部范围) | string | — | — |
extracss | 新的 css 样式表,并使用 分隔多个节点,(仅打印局部范围) | string | — | - |
poptitle | 标签内容(仅打印本地范围) | string | — | - |
opencallback | 调用打印工具成功回调函数 | function | returns the instance of vue called at that time | - |
closecallback | 关闭打印工具成功回调函数 | function | returns the instance of vue called at that time | - |
beforeopencallback | 调用打印工具前的回调函数 | function | returns the instance of vue called at that time | - |
url | 打印指定url。(不可同时设置id) | string | - | - |
asyncurl | 通过 'resolve()' 返回 url | function | - | - |
preview | 预览工具 | boolean | - | false |
previewtitle | 预览工具标题 | string | - | '打印预览' |
previewprintbtnlabel | 预览工具按钮的名称 | string | - | '打印' |
zindex | 预览工具的 css:z-index | string,number | - | 20002 |
previewbeforeopencallback | 启动预览工具前的回调函数 | function | returns the instance of vue | - |
previewopencallback | 预览工具完全打开后的回调函数 | function | returns the instance of vue | - |
官方文档:https://github.com/power-kxlee/vue3-print-nb?tab=readme-ov-file#v-print-api
到此这篇关于在vue3中使用vue3-print-nb实现前端打印功能的文章就介绍到这了,更多相关vue3前端打印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论