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

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

安装qrcodejs模块

npm i qrcodejs2 -S

样式文件 style.css

.qrcode {
    padding-top: 0.21333333rem;
    padding-bottom: 0.21333333rem;
}
.qrcode .qrcode_content {
    display: flex;
    justify-content: center;
}

封装成组件 qrcode.vue

<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>

创建容器、使用组件

<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>
目录
相关文章
|
1天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
1天前
|
JavaScript
Vue中避免滥用this去读取data中数据
Vue中避免滥用this去读取data中数据
|
1天前
|
JavaScript
vue中使用pinia及持久化
vue中使用pinia及持久化
4 0
|
1天前
|
JavaScript 前端开发 算法
Vue3与Vue2:对比分析与迁移指南
Vue3与Vue2:对比分析与迁移指南
|
1天前
|
JavaScript 前端开发 UED
Vue class和style绑定:动态美化你的组件
Vue class和style绑定:动态美化你的组件
|
1天前
|
JavaScript 前端开发 API
Vue 监听器:让你的应用实时响应变化
Vue 监听器:让你的应用实时响应变化
|
1天前
|
JavaScript
vue封装svg
vue封装svg
6 0
|
1天前
|
JavaScript
vue封装面包屑组件
vue封装面包屑组件
6 0
|
1天前
|
JavaScript
Vue 编写(preventReClick)防暴点 +防抖(debounce)和节流(throttle)函数
Vue 编写(preventReClick)防暴点 +防抖(debounce)和节流(throttle)函数
|
1天前
|
JavaScript 数据库
ant design vue日期组件怎么清空 取消默认当天日期
ant design vue日期组件怎么清空 取消默认当天日期