vue实现打印功能

2022-01-24  本文已影响0人  莫伊剑客

打印功能可以自己使用原生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
上一篇 下一篇

猜你喜欢

热点阅读