Vue qrcodejs2 生成二维码

Gtihub:https://github.com/davidshimjs/qrcodejs

Npm:https://www.npmjs.com/package/qrcodejs2

QRCode.js是用于制作二维码的JavaScript库。通过HTML5 Canvas和DOM中的表格标签支持跨浏览器。

安装

npm i qrcodejs2

基础使用

<div id="qrcode"></div>
<script type="text/javascript">
new QRCode(document.getElementById("qrcode"), "http://jindo.dev.naver.com/collie");
</script>
or with some options

<div id="qrcode"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
	text: "http://jindo.dev.naver.com/collie",
	width: 128,
	height: 128,
	colorDark : "#000000",
	colorLight : "#ffffff",
	correctLevel : QRCode.CorrectLevel.H
});
</script>
and you can use some methods

qrcode.clear(); // clear the code.
qrcode.makeCode("http://naver.com"); // make another code.

Vue

<template>
  <div class="qrcode-wrap" :ref="qrcodeRef"></div>
</template>

<script>
import QRCode from "qrcodejs2";

export default {
  name: "QRCode",
  data() {
    return {
      qrcodeRef: `qrcode-${this._uid}`,
      defaultOptions: {
        // width: "100%",
        // height: "100%",
        width: "128",
        height: "128",
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
      }
    };
  },
  props: {
    text: {
      type: [String],
      default: "",
      require: false
    },
    width: {
      type: [Number],
      default: 128,
      require: false
    },
    height: {
      type: [Number],
      default: 128,
      require: false
    },
    options: {
      type: [Object],
      default() {
        return {};
      },
      require: false
    }
  },
  computed: {
    qrcodeOptions() {
      return Object.assign(this.defaultOptions, {
        ...this.options,
        text: this.text,
        width: this.width,
        height: this.height
      });
    }
  },
  methods: {
    initQRCode() {
      this.qrcode = new QRCode(this.$refs[this.qrcodeRef], this.qrcodeOptions);
    },
    makeQRCode() {
      this.qrcode & this.qrcode.makeCode(this.text);
    },
    destroyQRCode() {
      this.qrcode && this.qrcode.clear();
      delete this.qrcode;
    }
  },
  watch: {
    text() {
      this.makeQRCode();
    },
    options() {
      this.initQRCode();
    }
  },
  mounted() {
    this.initQRCode();
  },
  destroyed() {
    this.destroyQRCode();
  }
};
</script>

<style></style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值