vue实现打印功能

打印功能可以自己使用原生js(window.print())实现,其次就是使用插件,介绍一个打印插件

vue-print-nb:这个插件使用起来还是很便捷的,用法如下
1、安装

// vue2.0
npm install vue-print-nb --save
// vue3.0
npm install vue3-print-nb --save

请根据自己的需求安装版本

2、配置
vue2.0

// 全局配置
import Print from 'vue-print-nb'
Vue.use(Print);

// 仅使用页面配置
import print from 'vue-print-nb'
directives: {
    print   
}

vue3.0

// 全局配置
import print from 'vue3-print-nb'
const app = createApp(App)
app.use(print)

// 仅使用页面配置
import print from 'vue3-print-nb'
directives: {
    print   
}

3、具体使用方法:(1)直接绑定id方法 (2)绑定对象方法

<!--直接绑定id方法-->
<div id='box'>
   <p>打印内容</p>
   <p>打印内容1111</p>
</div>
<div v-print="'#box'">打印</div>

<!--直接绑定对象方法-->
<div id='box'>
   <p>打印内容</p>
   <p>打印内容1111</p>
</div>
<div v-print='printObj'>打印</div>

export default{
   data(){
      return {
         printObj:{
            id:'box',
            beforeOpenCallback (vue) {
                console.log('打开之前')
            },
            openCallback (vue) {
                console.log('执行了打印')
            },
            closeCallback (vue) {
                console.log('关闭了打印工具')
            }
            //其他配置项,
         }
      }
   }
}

4、v-print API

Parame Explain Type OptionalValue DefaultValue
id id,required String — —
standard Document type (Print local range only) String html5/loose/strict html5
extraHead <head></head>Add DOM nodes in the node, and separate multiple nodes with (Print local range only), String — —
extraCss <link> New CSS style sheet , and separate multiple nodes with (Print local range only), String — —
popTitle <title></title> Content of label (Print local range only) String — —
openCallback Call the successful callback function of the printing tool Function Returns the instance of called at that timeVue —
closeCallback Close the callback function of printing tool success Function Returns the instance of called at that timeVue —
beforeOpenCallback Callback function before calling printing tool Function Returns the instance of called at that timeVue —
url Print the specified URL. (It is not allowed to set the ID at the same time) String — —
asyncUrl Return URL through 'resolve()' and Vue Function — —
preview Preview tool Boolean — false
previewTitle Preview tool Title String — '打印预览'
previewPrintBtnLabel The name of the preview tool button String — '打印'
zIndex CSS of preview tool: z-index String,Number — 20002
previewBeforeOpenCallback Callback function before starting preview tool Function Returns the instance of Vue —
previewOpenCallback Callback function after fully opening preview tool Function Returns the instance of Vue —
clickMounted Click the callback function of the print button Function Returns the instance of Vue —
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容