官网

https://github.com/davidshimjs/qrcodejs

1、引包

  1. npm install qrcodejs2 --save
  2. npm install --save html2canvas

2、引入

  1. import QRCode from 'qrcodejs2'
  2. import html2canvas from 'html2canvas'

3、生成二维码


tip:需要写一个定宽,不然生成图片会发生偏移和二维码不完整

  1. <div class="qrcode-img">
  2. <!-- <img src="../../assets/img/kyy/qrcode.jpg"> -->
  3. <div ref="qr" />
  4. </div>
  1. // 生成二维码
  2. bindQRCode () {
  3. let t = this
  4. // console.log(t.userInfo.account)
  5. this.$nextTick(() => {
  6. new QRCode(this.$refs.qr, {
  7. // text: 'http://192.168.0.xx:8765/#/SignAgency?code=' + t.userInfo.account,
  8. width: 200,
  9. height: 200,
  10. colorDark: '#333333', // 二维码颜色
  11. colorLight: '#ffffff', // 二维码背景色
  12. correctLevel: QRCode.CorrectLevel.L// 容错率,L/M/H
  13. })
  14. })
  15. }
  16. // 二维码生成后,执行生成图片并点击下载
  17. download () {
  18. html2canvas(this.$refs.qr, {
  19. backgroundColor: null,
  20. width: 189,
  21. height: 189
  22. }).then((canvas) => {
  23. const imgData = canvas.toDataURL('image/jpeg')
  24. console.log(imgData)
  25. const link = document.createElement('a')
  26. link.href = imgData
  27. link.download = 'qrCode.jpg'
  28. link.click()
  29. })
  30. }