vue-cli生成二维码

vue使用qrcode生成二维码

转载 https://blog.csdn.net/weixin_42890953/article/details/82776760

使用npm 安装插件

npm i qrcode --save

template 模板
qrCodeDialogVisible 加载弹窗显示值为 true 或 false
qrCodeHandleClose() 关闭弹窗回调函数 按需求设置
qrCodeCreate() 生成二维码函数

<template>
  <div class="hello">
		<el-dialog
		      title="请用微信扫描二维码支付押金"
		      :visible.sync="qrCodeDialogVisible"
		      @opened="qrCodeCreate"
		      width="20%"
		      :before-close="qrCodeHandleClose"
		    >
		      <div id="qrCode" class="myqrCode">
		        <div id="code"></div>
		        <canvas id="canvas"></canvas>
		      </div>
		    </el-dialog>
     </div>
  </template>

在使用页面引入插件即可

import QRCode from "qrcode";

export default {
  name: "HelloWorld",
  components: {
    QRCode: QRCode
  },
  data() {
    return {
        qrCodeDialogVisible: false,
    	qrTexxt: 'https://www.baidu.com'
    }
 }method(){
	qrCodeCreate() { // 生成二维码
      var _this = this;
      var canvas = document.getElementById("canvas");
      // _this.qrTexxt vue属性值为 https://www.baidu.com
      QRCode.toCanvas(canvas, _this.qrTexxt, function (error) {
        if (error) console.error(error);
        console.log("QRCode success!");
      });
    },
    qrCodeHandleClose() { //关闭弹窗
      this.qrCodeDialogVisible = false;
    }
 },
 mounted() {
    this.qrCodeDialogVisible=true; // 显示弹弹窗
  }
}

显示弹窗

open(){ // 打开弹窗
	this.qrCodeDialogVisible=true;
}

以上就可以生成不带图片的二维码
在这里插入图片描述

生成二维码就完成了,

有一个问题,动态 更换二维码内容是不刷新
open(){ // 打开弹窗
	this.qrText='https://editor.csdn.net/' // 给属性重新赋值
	this.qrCodeDialogVisible=true;
}

此时二维码显示空白,以下我的解决方案仅供参考
qrCodeCreate() 改进函数

qrCodeCreate() {
      var _this = this;
      document.getElementById("canvas").innerHTML = ""; // 每次加载,元素清空
      var canvas = document.getElementById("canvas");
     
     // 将属性拼接字符串即可
      var test = _this.qrtext + "";
      QRCode.toCanvas(canvas, test, function (error) {
        if (error) console.error(error);
        console.log("QRCode success!");
      });
    }

以上是生成二维码,有什么问题请在下方留言

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤来使用 vue-qr 生成二维码并下载二维码: 1. 安装 vue-qr 你可以使用以下命令来安装 vue-qr: ``` npm install vue-qr --save ``` 2. 在 Vue 组件中使用 vue-qr 你可以在 Vue 组件中使用 vue-qr,并传递需要生成二维码的文本作为参数: ```html <template> <div> <qr :text="qrText"></qr> <button @click="downloadQR">Download QR Code</button> </div> </template> <script> import QR from 'vue-qr' export default { components: { QR, }, data() { return { qrText: 'https://www.example.com', } }, methods: { downloadQR() { const canvas = document.querySelector('canvas') const dataURL = canvas.toDataURL('image/png') const link = document.createElement('a') link.download = 'qr-code.png' link.href = dataURL document.body.appendChild(link) link.click() document.body.removeChild(link) }, }, } </script> ``` 在上面的例子中,我们首先导入 vue-qr 并将其注册为组件。然后,我们定义了一个 data 属性 `qrText`,它包含了需要生成二维码的文本。在模板中,我们使用 `<qr>` 标签来渲染二维码。我们还添加了一个按钮,以便用户可以下载二维码。 在 `downloadQR()` 方法中,我们获取了 Canvas 元素并将其转换为 PNG 格式的数据 URL。然后,我们创建了一个 `<a>` 元素,并将其下载属性设置为 `qr-code.png`,并将其 `href` 属性设置为数据 URL。最后,我们将 `<a>` 元素添加到页面中,模拟用户单击该元素并将其从页面中删除。 3. 运行应用程序 现在,你可以运行你的应用程序并生成并下载二维码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值