vue3中使用print-js組件實(shí)現(xiàn)打印操作步驟
第一步:安裝依賴
yarn add print-js
第二步:創(chuàng)建打印組件:PrintHtmlComp.vue
<template> <div id="printArea_123456789"> <!-- 默認(rèn)插槽,傳入打印內(nèi)容 --> <slot></slot> </div> </template> <script setup lang="ts"> import printJs from 'print-js'; defineOptions({ name: 'PrintHtmlComp' }); const props = defineProps({ fontSize: { type: Number, default: 14, required: false, }, }); const handlePrint = () => { printJs({ printable: 'printArea_123456789', // 打印區(qū)域的id type: 'html', scanStyles: true, targetStyles: ['*'], // 使用dom的所有樣式,很重要 font_size: props.fontSize + 'px', // 字體大小 --很重要,這里字體要和css中字體大小保持一致,默認(rèn)是16px }); }; defineExpose({ handlePrint }); </script>
第三步:處理打印預(yù)覽的界面 PrintPop.vue
這個界面的的樣式調(diào)整直接關(guān)系到打印的樣式的,即想要打印什么樣的效果就在這個界面怎么調(diào)試。
<el-dialog v-model="state.dialogVisible" :destroy-on-close="true" :title="state.title" width="780px" @close="close"> <PrintHtmlComp ref="printRef"> <!---這里是想要打印的--> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> <div style="font-size: 20px">HELLO WORLD</div> </PrintHtmlComp> <template #footer> <el-button @click="close">取 消</el-button> <el-button :loading="state.saveLoading" type="primary" @click="handlePrint">打 印</el-button> </template> </el-dialog> <script lang="ts" setup> import { reactive, ref } from 'vue'; defineOptions({ name: 'WorkApplyPlanPrint' }); const state = reactive({ dialogVisible: false, }); const showEdit = async () => { state.dialogVisible = true; }; const close = () => { state.dialogVisible = false; }; // 打印功能的函數(shù) const handlePrint = async () => { printRef.value?.handlePrint(); }; defineExpose({ showEdit }); </script>
HTML展示效果
打印預(yù)覽效果:
完成,功德+1。
關(guān)于遇到的問題以及解決方法,后續(xù)更新?。。?!
到此這篇關(guān)于vue3中使用print-js組件實(shí)現(xiàn)打印操作的文章就介紹到這了,更多相關(guān)vue3中使用print-js組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli webpack 開發(fā)環(huán)境跨域詳解
本篇文章主要介紹了vue-cli webpack 開發(fā)環(huán)境跨域詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05vue實(shí)現(xiàn)將一個數(shù)組內(nèi)的相同數(shù)據(jù)進(jìn)行合并
今天小編就為大家分享一篇vue實(shí)現(xiàn)將一個數(shù)組內(nèi)的相同數(shù)據(jù)進(jìn)行合并,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11VUE 實(shí)現(xiàn)一個簡易老虎機(jī)的項(xiàng)目實(shí)踐
老虎機(jī)在很多地方都可以見到,可以設(shè)置中獎位置,以及中獎回調(diào),本文主要介紹了VUE 實(shí)現(xiàn)一個簡易老虎機(jī)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04vue-cli3+echarts實(shí)現(xiàn)漸變色儀表盤組件封裝
這篇文章主要為大家詳細(xì)介紹了vue-cli3+echarts實(shí)現(xiàn)漸變色儀表盤組件封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03