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

  1. 下载arale-qrcode依赖:
html 复制代码
npm i arale-qrcode --save
  1. 引用依赖:
html 复制代码
import AraleQRCode from "arale-qrcode";
  1. 代码部分:
html 复制代码
<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.

相关推荐
遇见火星14 分钟前
Docker入门:快速部署你的第一个Web应用
前端·docker·容器
WeilinerL31 分钟前
泛前端代码覆盖率探索之路
前端·javascript·测试
浮游本尊34 分钟前
React 18.x 学习计划 - 第五天:React状态管理
前端·学习·react.js
-睡到自然醒~40 分钟前
[go 面试] 前端请求到后端API的中间件流程解析
前端·中间件·面试
洛卡卡了1 小时前
Sentry 都不想接,这锅还让我背?这xx工作我不要了!
前端·架构
咖啡の猫1 小时前
Vue 实例生命周期
前端·vue.js·okhttp
JNU freshman1 小时前
vue 之 import 的语法
前端·javascript·vue.js
剑亦未配妥1 小时前
Vue 2 响应式系统常见问题与解决方案(包含_demo以下划线开头命名的变量导致响应式丢失问题)
前端·javascript·vue.js
爱吃的强哥1 小时前
Vue2 封装二维码弹窗组件
javascript·vue.js
凉柚ˇ1 小时前
Vue图片压缩方案
前端·javascript·vue.js