前端自动生成二维码并长按识别跳转 Vue

  1. 下载arale-qrcode依赖:
npm i arale-qrcode --save
  1. 引用依赖:
import AraleQRCode from "arale-qrcode";
  1. 代码部分:
<template>
  <div class="container">
    // 生成二维码按钮
	<van-button @click="getErweima">点击生成二维码</van-button>
	// 生成二维码弹窗
    <van-overlay :show="show">  
      <div class="wrapper">
          <img :src="img" />
      </div>
    </van-overlay>
  </div>
</template>

<script>
import AraleQRCode from "arale-qrcode";  //引入依赖
export default {
  name: "index",
  data() {
    return {
      show: false,
      img: "",
    };
  },
  mounted() {
  },
  methods: {
    makeCode() {
      const result = new AraleQRCode({
        render: "svg",
        text: "http://www.baidu.com",
        size: 120,
      });
      // 将svg xml文档转换成字符串
      const svgXml = new XMLSerializer().serializeToString(result);
      // 将svg字符串转成base64格式,通过 window.btoa方法创建一个 base-64 编码的字符串,进行二次编码解码(encodeURIComponent 字符串进行编码和解码,unescape 进行解码)。
      const src = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(svgXml)));
      localStorage.setItem("image", src);
      this.getImg();
    },

    // 获取存储的图片给到页面
    getImg() {
      this.img = localStorage.getItem("image");
      localStorage.removeItem("image");
    },
    getErweima() {
      this.show = true
      this.makeCode()
    },
  },
};
</script>
<style lang="scss" scoped></style>

Learning is like the fennel, but without its increase, it has its own advantages.

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值