VUE3基于vite封裝條形碼和二維碼組件的詳細(xì)過(guò)程
概要

基礎(chǔ)組件開發(fā)是項(xiàng)目業(yè)務(wù)開發(fā)的基石, 本文主要介紹了通過(guò)vue3的vite腳手架快速搭建項(xiàng)目, 開發(fā)條形碼和二維碼組件的過(guò)程。
使用vite創(chuàng)建項(xiàng)目
初始化下vue項(xiàng)目
npm init vite@latest然后按照提示操作即可!
創(chuàng)建好項(xiàng)目后,執(zhí)行如下命令啟動(dòng)項(xiàng)目:
npm install npm run dev
安裝依賴
執(zhí)行如下命令安裝開發(fā)所需依賴:
npm install element-plus -S npm install jsbarcode -S npm install qrcode -S
注冊(cè)element-plus組件庫(kù)
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')條形碼組件
在components目錄下創(chuàng)建Barcode.vue, 配置需要傳遞的props參數(shù), removeUndefinedProps 刪除未傳值的props屬性, 在生命周期鉤子函數(shù)onMounted()中執(zhí)行生成條形碼的函數(shù)render(), 完整的代碼如下:
<template>
<div>
<canvas ref="barcodeRef" v-show="valid"></canvas>
<div v-show="!valid">
<slot></slot>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, defineProps } from 'vue'
import JsBarcode from 'jsbarcode'
const props = defineProps({
value: [String, Number],
//選擇要使用的條形碼類型
format: {
type: [String],
default: "CODE39"
},
//設(shè)置條之間的寬度
width: {
type:[String, Number],
default: 3
},
height: {
type:[String, Number],
default: 100
},
//覆蓋顯示的文本
text: [String, Number],
//使文字加粗體或變斜體
fontOptions: {
type: [String],
default: "bold italic"
},
//設(shè)置文本的字體
font: [String, Number],
//設(shè)置文本的水平對(duì)齊方式
textAlign: [String],
//設(shè)置文本的垂直位置
textPosition: [String],
//設(shè)置條形碼和文本之間的間距
textMargin: [String, Number],
//設(shè)置文本的大小
fontSize: {
type:[String, Number],
default: 15
},
//設(shè)置條和文本的顏色
lineColor: [String],
//設(shè)置條形碼的背景
background: {
type:[String],
default:"#eee"
},
//設(shè)置條形碼周圍的空白邊距
margin: [String, Number],
// 是否在條形碼下方顯示文字
displayValue: {
type: [String, Boolean],
default: true
}
})
const settings = {
format: props.format,//選擇要使用的條形碼類型
width: props.width,//設(shè)置條之間的寬度
height: props.height,//高度
displayValue: props.displayValue,//是否在條形碼下方顯示文字
text: props.text,//覆蓋顯示的文本
fontOptions: props.fontOptions,//使文字加粗體或變斜體
font: props.font,//設(shè)置文本的字體
textAlign: props.textAlign,//設(shè)置文本的水平對(duì)齊方式
textPosition: props.textPosition,//設(shè)置文本的垂直位置
textMargin: props.textMargin,//設(shè)置條形碼和文本之間的間距
fontSize: props.fontSize,//設(shè)置文本的大小
background: props.background,//設(shè)置條形碼的背景
lineColor: props.lineColor,//設(shè)置條和文本的顏色。
margin: props.margin//設(shè)置條形碼周圍的空白邊距
}
const removeUndefinedProps = (obj) => {
for (let prop in obj) {
if (obj.hasOwnProperty(prop) && obj[prop] === undefined) {
delete obj[prop]
}
}
}
const valid = ref(true)
const barcodeRef = ref(null)
onMounted(() => {
removeUndefinedProps(settings)
render()
})
const render = () => {
JsBarcode(barcodeRef.value, props.value, settings)
}
</script>二維碼組件
在components目錄下創(chuàng)建Qrcode.vue, 代碼如下:
<template>
<canvas ref="qrcode"></canvas>
</template>
<script setup>
import { ref, onMounted, defineProps } from 'vue'
import QRCode from 'qrcode'
const props = defineProps({
value: {
type: String,
default: "https://baidu.com"
}
})
const qrcode = ref(null)
onMounted(() => {
render()
})
const render = () => {
QRCode.toCanvas(qrcode.value, props.value, (error) => {
if (error) {
console.log(error)
}
})
}
</script>App.vue代碼
使用element-plus組件庫(kù)的el-table組件展示條形碼, 代碼如下:
<template>
<el-table
:data="tableData"
border
style="width: 100%">
<el-table-column
type="index"
label="序號(hào)"
align="center"
width="60">
</el-table-column>
<el-table-column
prop="name"
label="品名"
align="center"
width="180">
</el-table-column>
<el-table-column
prop="code"
align="center"
label="編碼">
</el-table-column>
<el-table-column
align="center"
label="條形碼">
<template #default="scope">
<barcode :value="scope.row.code" />
</template>
</el-table-column>
<el-table-column
align="center"
label="二維碼">
<template #default="scope">
<qrcode :value="scope.row.url" />
</template>
</el-table-column>
</el-table>
</template>
<script setup>
import Barcode from './components/Barcode.vue'
import Qrcode from './components/Qrcode.vue'
const tableData = [
{ name: '蘋果', code: 'fruit1231', url: 'https://baidu.com' },
{ name: '香蕉', code: 'fruit1232', url: 'https://baidu.com' },
{ name: '橘子', code: 'fruit1233', url: 'https://baidu.com' }
]
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>效果如下:
到此這篇關(guān)于VUE3基于vite封裝條形碼和二維碼組件的詳細(xì)過(guò)程的文章就介紹到這了,更多相關(guān)vue3條形碼和二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)vue中v-on綁定自定事件的實(shí)例講解
今天小編就為大家分享一篇對(duì)vue中v-on綁定自定事件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
VUE 文字轉(zhuǎn)語(yǔ)音播放的實(shí)現(xiàn)示例
本文主要介紹了VUE 文字轉(zhuǎn)語(yǔ)音播放的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Vue+element自定義指令如何實(shí)現(xiàn)表格橫向拖拽
這篇文章主要介紹了Vue+element自定義指令如何實(shí)現(xiàn)表格橫向拖拽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue里input根據(jù)value改變背景色的實(shí)例
今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue Router中的冗余導(dǎo)航錯(cuò)誤及解決方案
在現(xiàn)代前端開發(fā)中,單頁(yè)應(yīng)用(SPA)已經(jīng)成為主流,而 Vue.js 作為一款流行的前端框架,其官方路由庫(kù) Vue Router 則是構(gòu)建 SPA 的核心工具之一,在使用 Vue Router 的過(guò)程中,開發(fā)者可能會(huì)遇到一些常見(jiàn)的錯(cuò)誤,本文將深入探討這一錯(cuò)誤的原因、影響以及解決方案2025-01-01
vuex 動(dòng)態(tài)注冊(cè)方法 registerModule的實(shí)現(xiàn)
這篇文章主要介紹了vuex 動(dòng)態(tài)注冊(cè)方法 registerModule的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Element?UI中v-infinite-scroll無(wú)限滾動(dòng)組件使用詳解
在移動(dòng)端數(shù)據(jù)的更新中許多方法孕育而生,無(wú)限滾輪也是解決的方案一種,Element-ui為vue開發(fā)了一個(gè)事件(v-infinite-scroll),下面這篇文章主要給大家介紹了關(guān)于Element?UI中v-infinite-scroll無(wú)限滾動(dòng)組件使用的相關(guān)資料,需要的朋友可以參考下2023-02-02
electron+vue?實(shí)現(xiàn)靜默打印功能
這篇文章主要介紹了electron+vue?實(shí)現(xiàn)靜默打印功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

