- 下载arale-qrcode依赖:
npm i arale-qrcode --save
- 引用依赖:
import AraleQRCode from "arale-qrcode";
- 代码部分:
<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.文章来源地址https://www.toymoban.com/news/detail-829325.html
文章来源:https://www.toymoban.com/news/detail-829325.html
到了这里,关于前端自动生成二维码并长按识别跳转 Vue的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!