直接上代码,借鉴了网上的代码,不要喷
<template class="aa">
<div>
<button class="notprint" @click="printOut(printDom)">
打 印
</button>
<div ref="printDom">
<hr />
<hr />
需打印的内容
<hr />
</div>
</div>
</template>
<script>
import {
reactive,
ref,
} from "vue";
export default {
name: "aa",
components: {},
setup(props) {
const printDom = ref(null);
const printOut = (dom) => {
var newWindow = window.open("打印页面", "_blank");
var docStr = dom.innerHTML
newWindow.document.write(docStr);
newWindow.document.close();
newWindow.print();
newWindow.close();
};
return {
printOut,
printDom
};
},
};
</script>
<style lang="less" scoped>
// 隐藏不需要打印的部分
@media print {
.notprint {
display: none;
}
}
</style>