vue3 vue3-print-nb 实现打印功能
效果
vue3-print-nb 文档
安装
pnpm add vue3-print-nb
typescript 中
xx.d.ts
declare module "vue3-print-nb";
配置
- 全局配置 src/main.ts
import print from "vue3-print-nb";const app = createApp(App);
app.use(print);
- 局部配置 xx.vue
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({// 打印 idid: "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>