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

相关推荐
undefined在掘金390419 小时前
flutter 仿商场_首页
前端
少卿9 小时前
react-native图标替换
前端·react native
熊猫钓鱼>_>9 小时前
TypeScript前端架构与开发技巧深度解析:从工程化到性能优化的完整实践
前端·javascript·typescript
敲敲敲敲暴你脑袋9 小时前
Canvas绘制自定义流动路径
vue.js·typescript·canvas
JYeontu10 小时前
肉眼难以分辨 UI 是否对齐,写个插件来辅助
前端·javascript
fox_10 小时前
别再踩坑!JavaScript的this关键字,一次性讲透其“变脸”真相
前端·javascript
盛夏绽放10 小时前
uni-app Vue 项目的规范目录结构全解
前端·vue.js·uni-app
少卿10 小时前
React Native Vector Icons 安装指南
前端·react native
国家不保护废物10 小时前
Vue组件通信全攻略:从父子传到事件总线,玩转组件数据流!
前端·vue.js
写不来代码的草莓熊11 小时前
vue前端面试题——记录一次面试当中遇到的题(9)
前端·javascript·vue.js