vue項目中使用qrcodesjs2生成二維碼簡單示例
最近寫項目遇到一個需求需要生成二維碼,看了一下項目中生成二維碼使用的都是qrcodesjs2,而且操作比較簡單。在這里簡單記錄一下
vue項目中使用qrcodesjs2生成二維碼
安裝
npm i qrcodejs2 -S
html
<!-- 放置二維碼的容器,需要給一個ref --> <div v-for="(item,i) in qrcode" :key="i"> <div id="qrcode" :ref="qrcode[i]"></div> <div>
項目中需要打印多個二維碼,而且這樣更加通用無論打印單個還是多個都可以
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生成多個二維碼
首先安裝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項目中使用qrcodesjs2生成二維碼的文章就介紹到這了,更多相關(guān)vue用qrcodesjs2生成二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue執(zhí)行配置選項npm?run?serve的本質(zhì)圖文詳解
本地開發(fā)一般通過執(zhí)行npm run serve命令來啟動項目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下2023-05-05Element的穿梭框數(shù)據(jù)量大時點擊全選卡頓的解決方案
本文主要介紹了Element的穿梭框數(shù)據(jù)量大時點擊全選卡頓的解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10解決vue使用vant下拉框van-dropdown-item 綁定title值不變問題
這篇文章主要介紹了解決vue使用vant下拉框van-dropdown-item 綁定title值不變問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08