Vue打印功能
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
这里介绍一个插件vue-print-nb蛮好用的用起来很方便所以想记录一下
npm官方 https://www.npmjs.com/package/vue-print-nb
安装
V2版本
npm install vue-print-nb --save
V3版本
npm install vue3-print-nb --save
引入
V2版本
main.js
import Print from 'vue-print-nb'
// Global instruction
Vue.use(Print);
//or
// Local instruction
import print from 'vue-print-nb'
directives: {
print
}
V3版本
main.js
// Global instruction
import { createApp } from 'vue'
import App from './App.vue'
import print from 'vue3-print-nb'
const app = createApp(App)
app.use(print)
app.mount('#app')
//or
// Local instruction
import print from 'vue3-print-nb'
directives: {
print
}
使用
目前我使用的是Vue2所以以V2为例介绍。vue3和vue2使用基本是一样的。
1. 打印整个页面
只需要添加 v-print 指令即可
<button v-print>Print the entire page</button>
2. 打印某个范围
我们要给打印的一个区域一个id然后在 v-print 指令指定该id即可
<button v-print="'#print-content'">打印</button>
<div id="print-content">
<p>葫芦娃葫芦娃</p>
<p>一根藤上七朵花 </p>
<p>小小树藤是我家 啦啦啦啦 </p>
<p>叮当当咚咚当当 浇不大</p>
<p> 叮当当咚咚当当 是我家</p>
<p> 啦啦啦啦</p>
<p>...</p>
</div>
3. 也可以给 v-print 指令设置对应的各个配置
参数 | 作用 | 类型 | 可选项 | 默认值 |
id | 局部打印有效标识符 | String | - | 'printId' |
standard | 局部打印有效打印的文本类型 | String | HTML5/loose/strict | HTML5 |
extraHead | 局部打印有效添加在打印区域的最顶端 | String | - | - |
extraCss | 局部打印有效为打印区域提供Stylesheet样式表 | String | - | - |
popTitle | 局部打印有效编辑页眉的标题 | String | - | Document Title |
clickMounted | 全局有效调用v-print绑定的按钮点击事件callback | Function | - | this.Object |
openCallback | 全局有效调用打印时的callback | Function | - | this.Object |
closeCallback | 全局有效调用关闭打印的callback(无法区分确认or取消) | Function | - | this.Object |
beforeOpenCallback | 全局有效调用开始打印之前的callback | Function | - | this.Object |
preview | 全局有效控制打印预览 | Boolean | true/false | false |
previewTitle | 编辑预览页面的预览标题 | String | - | ‘打印预览’ |
previewPrintBtnLabel | 编辑预览页面的打印按钮文本 | String | - | ‘打印’ |
previewBeforeOpenCallback | 调用打开预览页面之前的callback | Function | - | this.Object |
previewOpenCallback | 调用打开预览页面之后的callback | Function | - | this.Object |
url | 非局部打印有效打印指定的URL确保同源策略相同 | String | - | - |
asyncUrl | 非局部打印有效异步加载打印指定的URL确保同源策略相同 | Function | - | - |
zIndex | 预览有效预览窗口的z-index默认是20002最好比默认值更高 | StringNumber | - | 20002 |
<template>
<div class="main">
<button v-print="printObj" @click="print">打印</button>
<div id="print-content">
<p>葫芦娃葫芦娃</p>
<p>一根藤上七朵花 </p>
<p>小小树藤是我家 啦啦啦啦 </p>
<p>叮当当咚咚当当 浇不大</p>
<p> 叮当当咚咚当当 是我家</p>
<p> 啦啦啦啦</p>
<p>...</p>
</div>
<h2>哈哈哈哈哈哈</h2>
</div>
</template>
<script>
export default {
data() {
return {
printObj: {
id: 'print-content',
popTitle: 'good print', // 如果不设置 默认是没有值即 undifined
}
}
}
}
写的有点儿仓促基本使用应该够用了碰到复杂的需求或者其他的到时候再研究先记录一下。