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就不会。

相关推荐
杨航 AI10 分钟前
JEV的用法
java·前端·数据库
Amos_Web10 分钟前
Rspack 源码解析(三十五):rspack_error 如何用 miette 渲染带源码定位的错误链
前端
修炼的dance16 分钟前
OCR预处理测了108组,纠斜去噪有用,坑在顺序和力度
前端
独隅18 分钟前
WebStorm Windows 版 React / Next.js 实战替换指南
javascript·windows·webstorm
冷眸_22 分钟前
同名食材不能直接相减:一个 JavaScript 用量模型的边界
javascript
不可能片场25 分钟前
AI视频成片 帧间差分把拼接缝照出来
前端·electron
写的都是BUG25 分钟前
APK 里的钥匙,一直就放在门口:聊聊加固真正该做什么
前端·算法
dpharness27 分钟前
我踩过的四个记忆坑,从预算到换机
前端·后端
ruanno_top30 分钟前
倒计时该用 deadline 还是每秒减一?我把两种写法都跑了一遍
vue.js
apollo_qwe32 分钟前
接口超时了,前端为什么不能直接重试?
javascript·http