vue實現打印功能最簡單的方法

一、下載打印插件

通過這個地址下載vue打印插件:https://github.com/xyl66/vuePlugs_printjs ,下載完畢後,在src下新建文件夾plugs,將下載的插件print.js放到plugs文件夾裏

二、全局註冊打印插件

在main.js裏註冊打印插件,註冊完成後,就可以在組件中使用了,main.js裏的代碼如下:

// vue 打印插件
import Print from '@/plugs/print'
Vue.use(Print) 

三、使用打印插件

全局註冊完打印插件後,我們就可以在組件中使用它了,具體使用方法如下:

html部分:

<template>
    /*ref一定不要忽略*/
    <div ref="print">我要打印這個div裏的內容</div>
    <button @click="start_print">開始打印</button>  
</template>

js部分:

methods: {
    // 打印
    start_print(){
        this.$print(_this.$refs.print);//$refs的值要和html裏的ref一致
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章