1.安装 npm install print-js --save 2.在main.js中引入 import printJS from 'print-js' 使用
兼容IE11
html <el-button @click="printpage" >打印</el-button> <div id="print">要打印内容</div> js printpage () { // 方法一 在当前窗口打印, 问题:重新刷新页面,体验不友好 let subOutputRankPrint = document.getElementById('print') // console.log(subOutputRankPrint.innerHTML) let newContent = subOutputRankPrint.innerHTML let oldContent = document.body.innerHTML document.body.innerHTML = newContent window.print() window.location.reload() document.body.innerHTML = oldContent return false // 方法二 在新窗口打印,问题:无表格线 // var newWindow = window.open("打印窗口", "_blank"); // var docStr = document.getElementById("print").innerHTML; // newWindow.document.write(docStr); // var styles = document.createElement("style"); // styles.setAttribute('type', 'text/css'); //media="print" // styles.innerHTML = "" // newWindow.document.getElementsByTagName('head')[0].appendChild(styles); // newWindow.print(); // newWindow.close(); },