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

相关推荐
小周同学@1 小时前
谈谈对this的理解
开发语言·前端·javascript
Wiktok1 小时前
Pyside6加载本地html文件并实现与Javascript进行通信
前端·javascript·html·pyside6
一只小风华~1 小时前
Vue:条件渲染 (Conditional Rendering)
前端·javascript·vue.js·typescript·前端框架
柯南二号1 小时前
【大前端】前端生成二维码
前端·二维码
程序员码歌2 小时前
明年35岁了,如何破局?说说心里话
android·前端·后端
博客zhu虎康3 小时前
React Hooks 报错?一招解决useState问题
前端·javascript·react.js
灰海3 小时前
vue中通过heatmap.js实现热力图(多个热力点)热区展示(带鼠标移入弹窗)
前端·javascript·vue.js·heatmap·heatmapjs
王源骏3 小时前
LayaAir鼠标(手指)控制相机旋转,限制角度
前端
大虾写代码4 小时前
vue3+TS项目配置Eslint+prettier+husky语法校验
前端·vue·eslint
wordbaby4 小时前
用 useEffectEvent 做精准埋点:React analytics pageview 场景的最佳实践与原理剖析
前端·react.js