使用生成條形碼并打印和vue-print-nb以及報(bào)錯(cuò)問(wèn)題的解決
生成條形碼并打印和vue-print-nb以及報(bào)錯(cuò)
借鑒網(wǎng)上大多數(shù)vue-print-nb安裝打印,發(fā)現(xiàn)在自己的項(xiàng)目(ts語(yǔ)法下)中會(huì)發(fā)現(xiàn)模塊引入錯(cuò)誤
所以換成
npm install vue3-print-nb //重新安裝
vue-print-nb在自己的項(xiàng)目中無(wú)法使用,故此更換
引入后需要在根目錄下的env.d.ts中引入
declare module 'vue3-print-nb' {
const install: (app: App) => void
const print: (options?: PrintOptions) => void
export default { install, print }
}引入后需要在tsconfig.json文件里的include中添加env.d.ts的路徑
也可以在ts項(xiàng)目下的默認(rèn)下的types下的文件中引入也可以,或者全局搜索 declare module,在添加,這在樣mian.ts下引入 import print from 'vue3-print-nb' 不會(huì)報(bào)類(lèi)型報(bào)錯(cuò),在進(jìn)行全局掛載
使用條形碼多批量打印
<div class="preview-area" id="printTest">
<div v-for="(item, index) in data" :key="index">
<svg :ref="el => setBarcodeRef(el, index)"></svg>
<p style="page-break-after: always"></p>
</div>
</div>
<el-button v-print="printObj" ref="printBtn" style="display: none;"></el-button>
</div>
const data = ref([])
const barcodeRefs = ref([])
const printBtn = ref(null)
const printObj = reactive({
id: 'printTest',
beforeOpenCallback(vue) {
vue.printLoading = true
},
openCallback(vue) {
vue.printLoading = false
},
closeCallback(vue) {
console.log('關(guān)閉了打印工具')
}
})
//獲取每一個(gè)對(duì)應(yīng)的svg
const setBarcodeRef = (el, index) => {
barcodeRefs.value[index] = el
}
//list為數(shù)據(jù)源 調(diào)用showPrint進(jìn)行數(shù)據(jù)調(diào)用
const showPrint = async (list) => {
data.value = list
await nextTick()
await generateBarcode(list)
// 生成完成后自動(dòng)觸發(fā)打印
await nextTick()
printBtn.value.$el.click()
}
const generateBarcode = async (list) => {
try {
list.forEach((item, index) => {
if(barcodeRefs.value[index]) {
JsBarcode(barcodeRefs.value[index], item.LocationCode, {
format: "CODE128",
lineColor: "#000",
width: 2,
height: 60,
displayValue: true,
fontSize: 16,
margin: 10
});
}
})
} catch (error) {
console.error('生成條形碼失敗:', error);
}
};總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue-router路徑計(jì)算問(wèn)題
這篇文章主要介紹了關(guān)于vue-router路徑計(jì)算問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
詳解如何編寫(xiě)一個(gè)Vue3響應(yīng)式系統(tǒng)
這篇文章主要為大家學(xué)習(xí)介紹了如何編寫(xiě)一個(gè)Vue3響應(yīng)式系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
vue踩坑日記之params傳遞參數(shù)問(wèn)題
這篇文章主要介紹了vue踩坑日記之params傳遞參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

