官网
https://github.com/davidshimjs/qrcodejs
1、引包
npm install qrcodejs2 --save
npm install --save html2canvas
2、引入
import QRCode from 'qrcodejs2'
import html2canvas from 'html2canvas'
3、生成二维码
tip:需要写一个定宽,不然生成图片会发生偏移和二维码不完整
<div class="qrcode-img">
<!-- <img src="../../assets/img/kyy/qrcode.jpg"> -->
<div ref="qr" />
</div>
// 生成二维码
bindQRCode () {
let t = this
// console.log(t.userInfo.account)
this.$nextTick(() => {
new QRCode(this.$refs.qr, {
// text: 'http://192.168.0.xx:8765/#/SignAgency?code=' + t.userInfo.account,
width: 200,
height: 200,
colorDark: '#333333', // 二维码颜色
colorLight: '#ffffff', // 二维码背景色
correctLevel: QRCode.CorrectLevel.L// 容错率,L/M/H
})
})
}
// 二维码生成后,执行生成图片并点击下载
download () {
html2canvas(this.$refs.qr, {
backgroundColor: null,
width: 189,
height: 189
}).then((canvas) => {
const imgData = canvas.toDataURL('image/jpeg')
console.log(imgData)
const link = document.createElement('a')
link.href = imgData
link.download = 'qrCode.jpg'
link.click()
})
}