pnpm add vue3-print-nb
xx.d.ts
declare module "vue3-print-nb";
import print from "vue3-print-nb";
const app = createApp(App);
app.use(print);
import print from "vue3-print-nb";
const vPrint = print;
<script lang="ts" setup>
import { ref } from "vue";
import print from "vue3-print-nb";
// 配置指令
const vPrint = print;
</script>
<template>
<button v-print>打印</button>
</template>
<style lang="scss" scoped></style>
<script lang="ts" setup>
import { ref } from "vue";
import print from "vue3-print-nb";
// 配置指令
const vPrint = print;
</script>
<template>
<!-- 指定打印的范围 id -->
<div id="printMe">
<h2>打印部分</h2>
<img src="/test.jpg" style="width: 300px; height: 200px" alt="" />
</div>
<!-- 写入打印的 id 字符串格式 -->
<button v-print="'printMe'">打印</button>
</template>
<style lang="scss" scoped></style>
可配置标题,url ,预览,异步等
<script lang="ts" setup>
import { ref, reactive } from "vue";
import print from "vue3-print-nb";
// 配置指令
const vPrint = print;
const printObj = reactive({
// 打印 id
id: "printMe",
// 打印标题
popTitle: "打印标题",
beforeOpenCallback() {
console.log("打开之前");
},
openCallback() {
console.log("执行了打印");
},
closeCallback() {
console.log("关闭了打印工具");
},
});
</script>
<template>
<!-- 指定打印的范围 id -->
<div id="printMe">
<h2>打印部分</h2>
<img src="/test.jpg" style="width: 300px; height: 200px" alt="" />
</div>
<!-- 写入打印的 id 字符串格式 -->
<button v-print="printObj">打印</button>
</template>
<style lang="scss" scoped></style>