Vue3 realizes the area printing function (vue3-print-nb)

Scenes

Most background systems have printing needs. When there is a printing demand, it is of course easier for the front end to print the page directly. Then this article is in vue3, using the vue3-print-nb plug-in to print in the area. Where to hit!

1. Install vue3-print-nb

npm install vue3-print-nb

2. Introduce in main.ts

//引入
import print from 'vue3-print-nb'
//挂载
const app = createApp(App)
app.use(print)

Three.HTML

<!-- 打印区域容器 -->
<div id="printBox">
<span>我就是被打印的内容</span>
<span>在#printBox 容器里的内容都会被打印噢</span>
</div>

<!-- 按钮绑定打印 -->
<button v-print="print">点击打开打印预览</button>	

4. Parameter configuration

//这里是打印的配置项
const  print=ref({
        id: 'printBox',//这里的id就是上面我们的打印区域id,实现指哪打哪
        popTitle: '配置页眉标题', // 打印配置页上方的标题
        extraHead: '', // 最上方的头部文字,附加在head标签上的额外标签,使用逗号分割
        preview: false, // 是否启动预览模式,默认是false
        previewTitle: '预览的标题', // 打印预览的标题
        previewPrintBtnLabel: '预览结束,开始打印', // 打印预览的标题下方的按钮文本,点击可进入打印
        zIndex: 20002, // 预览窗口的z-index,默认是20002,最好比默认值更高
        previewBeforeOpenCallback() { console.log('正在加载预览窗口!'); }, // 预览窗口打开之前的callback
        previewOpenCallback() { console.log('已经加载完预览窗口,预览打开了!') }, // 预览窗口打开时的callback
        beforeOpenCallback() { console.log('开始打印之前!') }, // 开始打印之前的callback
        openCallback() { console.log('执行打印了!') }, // 调用打印时的callback
        closeCallback() { console.log('关闭了打印工具!') }, // 关闭打印的callback(无法区分确认or取消)
        clickMounted() { console.log('点击v-print绑定的按钮了!') },
 
      })

 


recommended article

Print.js realizes printing pdf, HTML, pictures (styles can be set and paged) 

Quickly understand Pinia and data persistent storage (detailed tutorial)

Use qrcodejs2-fix plugin to generate QR code in vue3

Nginx deploys front-end static website detailed teaching (step by step)

The front end realizes the export and download of excel files (Blob data type)

Guess you like

Origin blog.csdn.net/G_ing/article/details/128164245