目录安装 引入 使用 这里介绍一个插件(Vue-print-nb),蛮好用的,用起来很方便,所以想记录一下 npm官方: https://w
这里介绍一个插件(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 指令设置对应的各个配置:
<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
}
}
}
}
写的有点儿仓促,基本使用应该够用了,碰到复杂的需求或者其他的到时候再研究,先记录一下。
到此这篇关于Vue利用插件实现打印功能的示例详解的文章就介绍到这了,更多相关Vue打印内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Vue利用插件实现打印功能的示例详解
本文链接: https://www.lsjlt.com/news/200219.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0