关键代码如下:
function printTable() {
// setTimeout可以不用,我这里是做了其他操作,所以做了一个异步操作
setTimeout(() => {
//获取要打印的页面部分
const printHtml = document.getElementById('printContent').innerHTML
//打开一个新窗口进行打印,直接调用windo.print打印会有bug,
const win = window.open('', 'newwindow', 'height=600, width=1000, top=100, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
//将获取到的页面部分插入到新窗口
win.document.body.innerHTML = printHtml
// 设置样式,
const style = win.document.createElement('style')
style.innerHTML = getStyle()
win.document.body.appendChild(style)
win.print()
}, 50)
return false
},
function getStyle() {
// 支持css,不支持less和scss
return `.print-page {
background-color: #eee;
}`
}
SCSS转CSS:
如果是scss,附上一个scss转css的工具,完美转换
https://www.sassmeister.com/