<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vue使用qrcodejs绘制二维码</title>
<script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script>
<style>
.qrCode {
border-radius: 10px;
box-shadow: 0 0 2px 3px #eeeeee;
padding: 10px;
margin: 10px auto;
}
.title {
text-align: center;
}
.qrCodeContainer {
margin: auto;
width: 300px;
height: 300px;
}
/* 重写二维码尺寸 */
.qrCodeContainer canvas+img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="app">
<div class="qrCode" v-for="(item, index) in cardId" :key="index">
<p class="title" v-text="item"></p>
<div class="qrCodeContainer" :ref="'qrCodeDiv'+index"></div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
cardId: ['Cheryl71','Daaayyy71']
},
mounted() {
this.$nextTick(() => {
for(let i=0; i<this.cardId.length; i++) {
this.creatQrCodeArray(i, this.cardId[i])
}
})
},
methods: {
creatQrCodeArray(index, cardId) {
let key = 'qrCodeDiv'+index;
new QRCode(this.$refs[key][0], {
text: cardId,
width: 200,
height: 200,
colorDark: "#333333", //二维码颜色
colorLight: "#ffffff", //二维码背景色
correctLevel: QRCode.CorrectLevel.L//容错率,L/M/H
})
}
}
})
</script>
</body>
</html>