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一致
    }
}

猜你喜欢

转载自blog.csdn.net/jiangwei1994/article/details/86612718