zhangax2008
管理员
管理员
  • UID1
  • 粉丝0
  • 关注0
  • 发帖数26
阅读:3442回复:0

vue实现打印功能的两种方法

楼主#
更多 发布于:2020-07-22 14:18
第一种方法:通过npm 安装插件
1,安装  npm install vue-print-nb --save
2,引入  安装好以后在main.js文件中引入
         import Print from 'vue-print-nb'
    Vue.use(Print);  //注册
3,现在就可以使用了


 <div id="printTest" >

      <p>明月照于山间</p>

      <p>清风来于江上 </p>

    </div>

    <button v-print="'#printTest'">打印</button>  





4.如需通过链接地址打印:window.location.href = airway_bill;  airway_bill为链接地址。
5.如果内容打印不全,在打印操作时点击更多设置,然后设置缩放。
 
第二种方法:手动下载插件到本地
插件地址:https://github.com/xyl66/vuePlugs_printjs
1.在src下新建文件夹plugs,将下载好的print.js放入plugs文件夹下,然后操作如下



mport Print from '@/plugs/print'
Vue.use(Print) // 注册
<template>
  <section ref="print">
    打印内容
    <div class="no-print">不要打印我</div>
  </section>
</template>
this.$print(this.$refs.print) // 使用





2.注意事项 需使用ref获取dom节点,若直接通过id或class获取则webpack打包部署后打印内容为空
3.指定不打印区域
 方法1. 添加no-print样式类
<div class="no-print">不要打印我</div>
方法2. 自定义类名
<div class="do-not-print-me-xxx">不要打印我</div>
this.$print(this.$refs.print,{'no-print':'.do-not-print-me-xxx'}) // 使用
游客


返回顶部