vue2、vue3生成二维码

Vue2版:

工具:使用 qrcodejs插件来生成二维码

安装:npm install qrcodejs2

**qrcodejs**官网地址:

https://davidshimjs.github.io/qrcodejs/https://davidshimjs.github.io/qrcodejs/

代码示例:

javascript 复制代码
<template>
  <div class="qrcodeBox">
    <div ref="qrcode" class="qrcode"></div>
    <button @click="generateQRCode">Generate QR Code</button>
  </div>
</template>
  
  <script>
import QRCode from "qrcodejs2";

export default {
  data() {
    return {
      text: "这是成的二维码内容", // 二维码内容
    };
  },
  mounted() {
    this.generateQRCode(); // 组件挂载后生成二维码
  },
  methods: {
    // 更新二维码
    generateQRCode() {
      // 清空二维码容器
      const container = this.$refs.qrcode;
      container.innerHTML = "";
      // 使用qrcodejs生成二维码
      new QRCode(container, {
        text: this.text,
        width: 128,
        height: 128,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.L,
      });
    },
  },
};
</script>
  
  <style>
.qrcodeBox {
  text-align: center;
  padding: 20px;
}
.qrcode {
  margin-bottom: 20px;
}
</style>
  

Vue3版:

把安装命令换为:

npm install qrcodejs2-fix

安装后使用方式没变

**tip:**在vue3中使用npm install qrcodejs2安装使用会报错(报错信息自己可以找找原因),使用 npm install qrcodejs2-fix就不会。

相关推荐
IT_陈寒7 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
ct9787 小时前
Three.js 性能优化(测量-定位-优化)
javascript·性能优化·three
研☆香7 小时前
es6新特性功能介绍(一)
前端·ecmascript·es6
陈_杨7 小时前
鸿蒙开发-疾阅App阅读训练功能技术解析
前端·javascript
zhangxingchao8 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端
凌涘8 小时前
依托 BEM 规范深度剖析 WeUI 微信按钮组件开发与实现
前端·微信
不好听6138 小时前
Node.js 工程化开发流程 — 知识点总结
javascript·node.js
小KK_8 小时前
CSS浮动布局指南:从文档流到BFC
前端·css·html
ZengLiangYi8 小时前
sql.js WASM 深度解析
javascript·数据库·后端