vue項(xiàng)目中使用qrcodesjs2生成二維碼簡(jiǎn)單示例
最近寫項(xiàng)目遇到一個(gè)需求需要生成二維碼,看了一下項(xiàng)目中生成二維碼使用的都是qrcodesjs2,而且操作比較簡(jiǎn)單。在這里簡(jiǎn)單記錄一下
vue項(xiàng)目中使用qrcodesjs2生成二維碼
安裝
npm i qrcodejs2 -S
html
<!-- 放置二維碼的容器,需要給一個(gè)ref --> <div v-for="(item,i) in qrcode" :key="i"> <div id="qrcode" :ref="qrcode[i]"></div> <div>
項(xiàng)目中需要打印多個(gè)二維碼,而且這樣更加通用無(wú)論打印單個(gè)還是多個(gè)都可以
js
// 生成二維碼 printTwoCode(width) { for (let j in this.qrcode) { let code = this.qrcode[j]; // 二維碼內(nèi)容 new QRCode(this.$refs[`${this.qrcode[j]}`][0], { text: code, render: 'canvas', width: width, height: width, colorDark: '#000000', colorLight: '#ffffff' }); } },
清除
for (let j in this.qrcode) { this.$refs.qrcode[j][0].innerHTML = '' }
使用
this.$nextTick(() => { this.printTwoCode(130); });
附:使用qrcodejs2生成多個(gè)二維碼
首先安裝qrcodejs2
然后引用 import QRCode from 'qrcodejs2'
<div class="box"> ? ? <div v-for="(item, index) in list" class="boxscod" :key="index"> ? ? ? <div :id="`code${item.id}`" :ref="`code${item.id}`" class="qrcode"> ? ? ? </div> ? ? ? <div class="abc"> ? ? ? ? ? <span class="cargo-wrap">{{ item.id }}</span> ? ? ? ? ? <span class="cargo-cardNo">{{ item.idCard }}</span> ? ? ? ? </div> ? ? </div> ? </div> return { ? ? ? list: [ ? ? ? ? { id: '01', idCard: 15336 }, ? ? ? ? { id: '02', idCard: 18528 }, ? ? ? ? { id: '03', idCard: 78542 }, ? ? ? ? { id: '04', idCard: 46824 }, ? ? ? ? ], ? ? }; mounted() { ? ? this.showCode(); ? }, ? methods: { ? ? creatQrCode(id, code) { ? ? ? console.log(code); ? ? ? console.log(typeof code); ? ? ? var qrcode = new QRCode(id, { ? ? ? ? text: encodeURI(code), // 需要轉(zhuǎn)換為二維碼的內(nèi)容 ? ? ? ? width: 70, ? ? ? ? height: 70, ? ? ? ? colorDark: "#000000", ? ? ? ? colorLight: "#ffffff", ? ? ? ? // correctLevel: QRCode.CorrectLevel.H, ? ? ? }); ? ? ? console.log(qrcode); ? ? }, ? ? // 展示二維碼 ? ? showCode() { ? ? ? // ? this.closeCode() ? ? ? this.$nextTick(() => { ? ? ? ? this.list.forEach((item) => { ? ? ? ? ? // ? if (item.type === 1 || item.type === 2) { ? ? ? ? ? this.creatQrCode("code" + item.id, item.idCard); ? ? ? ? ? // ? } ? ? ? ? }); ? ? ? }); ? ? }, ? },
總結(jié)
到此這篇關(guān)于vue項(xiàng)目中使用qrcodesjs2生成二維碼的文章就介紹到這了,更多相關(guān)vue用qrcodesjs2生成二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vuex利用state保存新聞數(shù)據(jù)實(shí)例
本篇文章主要介紹了Vuex利用state保存新聞數(shù)據(jù)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)圖文詳解
本地開發(fā)一般通過(guò)執(zhí)行npm run serve命令來(lái)啟動(dòng)項(xiàng)目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下2023-05-05vue實(shí)現(xiàn)實(shí)時(shí)搜索顯示功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)實(shí)時(shí)搜索顯示功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Element的穿梭框數(shù)據(jù)量大時(shí)點(diǎn)擊全選卡頓的解決方案
本文主要介紹了Element的穿梭框數(shù)據(jù)量大時(shí)點(diǎn)擊全選卡頓的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10解決vue使用vant下拉框van-dropdown-item 綁定title值不變問(wèn)題
這篇文章主要介紹了解決vue使用vant下拉框van-dropdown-item 綁定title值不變問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08