安装依赖
npm install print-js --save
引用
// 1,按需引入
import print from 'print-js'
//2, 全局挂载
import printJS from "print-js";
Vue.prototype.printJS = printJS;
例子
// html
<template>
<div id="printBox" @click="toPrint">
// 打印内容主题
......
</div>
</template>
// script
mounted(){
toPrint(){
that.$nextTick(() => {
printJS({
printable: "printBox", // 对应html 标签 id
type: "html",
css: ["./static/print.css"], // 引用对应打印文件的样式
style:'@page{size:auto;margin: 0cm 1cm 0cm 1cm;}',//去除页眉页脚
scanStyles: false,
});
that.printShow = false; // 控制打印标签(默认不显示)
});
}
}