vue中使用qrcode2js插件生成二维码并下载

需求:

本项目基于vue2+antd,在组件外点击按钮弹框,弹框内容见图。点击下载按钮保存二维码 

实现步骤:

1、安装qrcode2js 插件

npm install qrcodejs2 --save

2、直接上组件吧:

<template>
  <div>
    <a-modal :title="title" width="300px" :visible="visible || false" :ok-text="'下载二维码'"   :cancel-text="'关闭'" @ok="downloadCode" @cancel="handleCancel">
      <a id="downloadQRCODE"><div class="qrcode-container" :ref="id" style="width: 100%;height: 100%;" :id="id"></div></a>
    </a-modal>

  </div>
</template>
<script>
import QRCode from 'qrcodejs2'
export default {
  name: 'QRCode2',
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      required: true,
      default: 'qrcode'
    },
    title: {
      type: String,
      default: '查看二维码'
    },
    text: {  // 后端返回的二维码地址
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '200'
    },
    height: {
      type: String,
      default: '200'
    },
    colorDark: {
      type: String,
      default: '#000000'
    },
    colorLight: {
      type: String,
      default: '#ffffff'
    }
  },
  data () {
    return {
      qrcodeImg: ''
    }
  },
  watch: {
    text: {
      immediate: false,
      handler (obj) {
        this.createQrcode()
      }
    }
  },
  mounted () {
  },
  methods: {
    handleCancel () {
      this.$emit('onClose', true)
    },
    createQrcode () {
      let qrcodeDiv = document.getElementById(this.id || 'qrcode')
      if (!qrcodeDiv) {
        this.$nextTick(() => {
          qrcodeDiv = document.getElementById(this.id || 'qrcode')
          if (qrcodeDiv) {
            this.createQrcodeNext(qrcodeDiv)
          }
        });
      }else{
        this.createQrcodeNext(qrcodeDiv)
      }

    },
    createQrcodeNext (qrcodeDiv) {
      if (this.qrcodeImg) {  // 有新的二维码地址了,先把之前的清除掉
        this.$refs[this.id || 'qrcode'].innerHTML = ''
      }
      this.qrcodeImg = new QRCode(qrcodeDiv, {
        text: this.text, //页面地址 ,如果页面需要参数传递请注意哈希模式#
        width: this.width, // 二维码宽度 (不支持100%)
        height: this.height, // 二维码高度 (不支持100%)
        colorDark: this.colorDark,
        colorLight: this.colorLight,
        correctLevel: QRCode.CorrectLevel.H,
      })
    },
    downloadCode(){
      const nodeList = Array.prototype.slice.call(this.qrcodeImg._el.children)
      const img = nodeList.find((item) => item.nodeName.toUpperCase() === 'IMG')	// 选出图片类型
      // 构建画布
      let canvas = document.createElement('canvas');
      canvas.width = img.width;
      canvas.height = img.height;
      canvas.getContext('2d').drawImage(img, 0, 0);
      // 构造url
      let url = canvas.toDataURL('image/png');
      document.getElementById('downloadQRCODE').setAttribute('href', url);
      document.getElementById('downloadQRCODE').setAttribute('download', `${this.title}-二维码.png`);
      document.getElementById('downloadQRCODE').click();
    }
  }
}
</script>
<style lang="less" scoped>
.qrcode-container{
  width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>

3、调用:

visiable:控制模态框显示隐藏

id:随便给一个

title:弹框名称

text:二维码的内容,别人扫码扫出来的字符串

其他参数:二维码高宽啥的见上面组件中的props

<QrCode  
:visible="true" 
:id="'QrCode'" 
:title="`弹框名称`" 
:text="`二维码内容`" 
@onClose="()=>qrVisible=false"/>

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Vue使用qrcode生成二维码,你可以按照以下步骤进行操作: 1. 首先,你需要安装qrcode插件,你可以使用npm命令来安装:npm i qrcode -S 。 2. 然后,在你的Vue组件引入qrcode插件:import QRCode from 'qrcode' 。 3. 接下来,在你的Vue组件的template添加一个img标签,用于显示生成的二维码图片。例如: ``` <template> <div> <img :src="QRImgUrl" /> </div> </template> ``` 4. 在Vue组件的script使用QRCode生成二维码的方法。首先,你需要定义一个data属性QRImgUrl用于存储生成的二维码图片的URL。然后,在created钩子函数调用getQRcode方法来生成二维码。getQRcode方法使用QRCode.toDataURL来生成二维码图片的DataURL,并将生成的URL赋值给QRImgUrl。例如: ``` <script> import QRCode from 'qrcode' export default { data() { return { QRImgUrl: '', QRlink:'www.xxx.com' } }, created() { this.getQRcode() }, methods: { getQRcode(){ QRCode.toDataURL(this.QRlink, { errorCorrectionLevel: 'L', margin: 2, width: 128 }, (err, url) => { if (err) throw err this.QRImgUrl = url }) } } } </script> ``` 这样,当你的Vue组件被创建时,getQRcode方法会被调用,生成二维码并将URL赋值给QRImgUrl,从而显示在页面上 。 如果你想对生成的二维码进行更详细的配置,你可以参考以下步骤: 1. 在getQRcode方法定义一个opts对象,用于配置生成二维码的各种参数,比如容错级别、二维码类型、二维码质量、留白边距等 。 2. 修改QRCode.toDataURL方法的第二个参数为opts,这样可以根据opts的配置生成更加符合你需求的二维码 。 例如,你可以按照以下方式配置opts对象: ```javascript let opts = { errorCorrectionLevel: "L",//容错级别 type: "image/png",//生成的二维码类型 quality: 0.3,//二维码质量 margin: 5,//二维码留白边距 width: 128,//宽 height: 128,//高 text: "http://www.xxx.com",//二维码内容 color: { dark: "#666666",//前景色 light: "#fff"//背景色 } }; ``` 然后,将opts作为QRCode.toDataURL方法的第二个参数传入: ```javascript QRCode.toDataURL(this.QRlink, opts, (err, url) => { if (err) throw err this.QRImgUrl = url }) ``` 这样,你就可以根据opts的配置生成定制化的二维码 。 综上所述,你可以按照以上步骤在Vue使用qrcode生成二维码,并根据需要进行详细的配置 。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值