前端自动生成二维码并长按识别跳转 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.

你可能感兴趣的:(二维码,前端,vue.js)