首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【vue】 vue2 qrcodejs2链接生成二维码

【vue】 vue2 qrcodejs2链接生成二维码

作者头像
fruge365
发布2025-12-15 09:45:15
发布2025-12-15 09:45:15
1300
举报

安装qrcodejs模块

代码语言:javascript
复制
npm i qrcodejs2 -S

样式文件 style.css

代码语言:javascript
复制
.qrcode {
    padding-top: 0.21333333rem;
    padding-bottom: 0.21333333rem;
}

.qrcode .qrcode_content {
    display: flex;
    justify-content: center;
}

封装成组件 qrcode.vue

代码语言:javascript
复制
<template>
  <div class="qrcode">
    <div class="qrcode_content" :id="name"></div>
  </div>
</template>
<script>
import QRcode from "qrcodejs2";
let qrcode = "";
export default {
  name: "qrcode",
  data() {
    return {};
  },
  computed: {
    name: {
      get() {
        let sid = "qrcode";
        if (this.sid) {
          sid = this.sid;
        }
        return sid;
      },
      set(value) {
        this.name = value;
      },
    },
    value: {
      get() {
        let value =
          "https://xxxxxxxx/xxxxxxxxx"; //要生成二维码的链接
        if (this.text) {
          value = this.text;
        }
        return value;
      },
      set(value) {
        this.value = value;
      },
    },
    qrwidth() {
      let width = 0;
      if (this.swidth) {
        width = this.swidth; //370*320/(window.innerWidth)/20
      } else {
        width = 5.33; //250*320/(window.innerWidth)/20
      }
      return width;
    },
  },
  props: {
    sid: {
      type: String,
    },
    text: {
      type: String,
    },
    swidth: {
      type: Number,
    },
  },
  methods: {
    qrcode() {
      if (qrcode) {
        qrcode = null;
      } else {
        qrcode = new QRcode(this.name, {
          width: (this.qrwidth * 20 * window.innerWidth) / 320,
          height: (this.qrwidth * 20 * window.innerWidth) / 320, // 高度  ,250*320/(window.innerWidth)/20
          text: this.value, // 二维码内容
          image: "",
          correctLevel: QRcode.CorrectLevel.L,
          //容错级别,可设置为:(低到高)
          // QRCode.CorrectLevel.L
          // QRCode.CorrectLevel.M
          // QRCode.CorrectLevel.Q
          // QRCode.CorrectLevel.H
          // render: 'canvas' // 设置渲染方式(有两种方式 table和canvas,默认是canvas)
          // background: '#f0f'
          // foreground: '#ff0'
        });
      }
      console.log(qrcode);
    },
  },
  destroyed() {
    qrcode = null;
  },
  mounted() {
    this.qrcode();
  },
};
</script>
<style lang="scss" scoped>
// 注意修改文件路径
@import "../assets/style.css";
</style>

创建容器、使用组件

代码语言:javascript
复制
<template>
  <div>
    <Qrcode sid="url" :text="codeText" :swidth="swidth"></Qrcode>
  </div>
</template>
<script>
// 注意修改文件路径
import Qrcode from "./qrcode.vue";
export default {
  data() {
    return {
      url: "",
      codeText: "",
    };
  },
  components: {
    Qrcode,
  },
  computed: {
    swidth() {
      return (window.innerWidth * 0.9 * 0.4 * 100) / window.innerWidth / 20; //370*320/(window.innerWidth)/20
    },
  },
  mounted() {
    this.url = this.QrcodeUrl;
    this.codeText = this.QrcodeText;
  },
};
</script>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装qrcodejs模块
  • 样式文件 style.css
  • 封装成组件 qrcode.vue
  • 创建容器、使用组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档