最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2023年08月17日 09:25:53   作者:Elwin0204  
基礎(chǔ)組件開發(fā)是項目業(yè)務(wù)開發(fā)的基石, 本文主要介紹了通過vue3的vite腳手架快速搭建項目, 開發(fā)條形碼和二維碼組件的過程,感興趣的朋友跟隨小編一起看看吧

概要

基礎(chǔ)組件開發(fā)是項目業(yè)務(wù)開發(fā)的基石, 本文主要介紹了通過vue3的vite腳手架快速搭建項目, 開發(fā)條形碼和二維碼組件的過程。

使用vite創(chuàng)建項目

初始化下vue項目

npm init vite@latest然后按照提示操作即可!
創(chuàng)建好項目后,執(zhí)行如下命令啟動項目:

npm install
npm run dev

安裝依賴

執(zhí)行如下命令安裝開發(fā)所需依賴:

npm install element-plus -S
npm install jsbarcode -S
npm install qrcode -S

注冊element-plus組件庫

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è)置文本的水平對齊方式
  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è)置文本的水平對齊方式
  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組件庫的el-table組件展示條形碼, 代碼如下:

<template>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      type="index"
      label="序號"
      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ān)vue3條形碼和二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3中vite使用sass的配置方法

    vue3中vite使用sass的配置方法

    這篇文章主要介紹了vue3中vite使用sass的配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • vue.js父子組件通信動態(tài)綁定的實例

    vue.js父子組件通信動態(tài)綁定的實例

    今天小編就為大家分享一篇vue.js父子組件通信動態(tài)綁定的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 關(guān)于Vue3中的響應(yīng)式原理

    關(guān)于Vue3中的響應(yīng)式原理

    這篇文章主要介紹了關(guān)于Vue3中的響應(yīng)式原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 解決前后端分離 vue+springboot 跨域 session+cookie失效問題

    解決前后端分離 vue+springboot 跨域 session+cookie失效問題

    這篇文章主要介紹了前后端分離 vue+springboot 跨域 session+cookie失效問題的解決方法,解決過程也很簡單 ,需要的朋友可以參考下
    2019-05-05
  • Vue實現(xiàn)開始時間和結(jié)束時間范圍查詢

    Vue實現(xiàn)開始時間和結(jié)束時間范圍查詢

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)開始時間和結(jié)束時間的范圍查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue中定義的data為什么是函數(shù)

    vue中定義的data為什么是函數(shù)

    這篇文章主要介紹了vue中定義的data為什么是函數(shù),vue中已經(jīng)幫我們控制臺輸出警告,并且不會讓組件中的data合并到options中去,那么,很友好的處理了開發(fā)者的強行將data寫成對象的可能性,需要的朋友可以參考下
    2022-09-09
  • vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)

    vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)

    這篇文章主要介紹了vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • vue3不能使用history.pushState修改url參數(shù)踩坑

    vue3不能使用history.pushState修改url參數(shù)踩坑

    這篇文章主要為大家介紹了vue3不能使用history.pushState修改url參數(shù)踩坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 在nuxt中使用路由重定向的實例

    在nuxt中使用路由重定向的實例

    這篇文章主要介紹了在nuxt中使用路由重定向的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue組件中使用防抖函數(shù)的方式

    vue組件中使用防抖函數(shù)的方式

    這篇文章主要介紹了vue組件中使用防抖函數(shù)的方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評論

富宁县| 五峰| 连云港市| 长子县| 大城县| 顺平县| 四会市| 尚志市| 平潭县| 太谷县| 信丰县| 封开县| 禹城市| 白水县| 灌南县| 康马县| 浦县| 通城县| 安岳县| 台南市| 望江县| 平凉市| 巍山| 林西县| 吴旗县| 永仁县| 平定县| 三河市| 宁海县| 乌兰浩特市| 永康市| 永春县| 游戏| 建水县| 饶河县| 涞源县| 保靖县| 定兴县| 绥江县| 沂南县| 尚义县|