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)文章
vue.nextTick()與setTimeout的區(qū)別及說(shuō)明
這篇文章主要介紹了vue.nextTick()與setTimeout的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue3 選中對(duì)話框時(shí)對(duì)話框右側(cè)出一個(gè)箭頭效果
本文主要介紹了Vue3實(shí)現(xiàn)選中對(duì)話框帶箭頭效果的方法,首先通過(guò)后臺(tái)獲取數(shù)據(jù)進(jìn)行遍歷,利用ts代碼判斷選中下標(biāo)與循環(huán)游標(biāo)是否一致以改變樣式,感興趣的朋友一起看看吧2024-10-10
基于vue2實(shí)現(xiàn)簡(jiǎn)單的答題組件
這篇文章主要為大家詳細(xì)介紹了如何基于vue2實(shí)現(xiàn)簡(jiǎn)單的答題組件,點(diǎn)擊正確的選項(xiàng),該選項(xiàng)背景變綠色;點(diǎn)擊錯(cuò)誤的選項(xiàng),該選項(xiàng)背景變紅色,有需要的可以參考下2024-12-12
vue.js將時(shí)間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue.js將時(shí)間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
打通前后端構(gòu)建一個(gè)Vue+Express的開發(fā)環(huán)境
這篇文章主要介紹了打通前后端構(gòu)建一個(gè)Vue+Express的開發(fā)環(huán)境,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

