前言:pc端实现微信分享,本人没有找到其他办法,通过生成二维码实现的,如果有其它更好的实现方式,请大佬指教
第一步
下载生成二维码的插件
cnpm install qrcodejs2 -S
第二步
引入该插件
import QRCode from 'qrcodejs2';
第三步
使用
// 准备生成二维码的容器
<template>
<div id="qrcode" ref="qrcode"></div>
<button @click="creatQrCode">生成二维码</button>
</template>
// 使用
<script>
import QRCode from 'qrcodejs2';
export default {
data() {
return {
qrCodeURL: '', // 二维码地址
temporaryVar: null,
};
},
methods: {
// 生成二维码
creatQrCode() {
if (this.temporaryVar) {
this.temporaryVar.clear(); //清除二维码
this.$refs['qrcode'].innerHTML = ''; //清除二维码
this.temporaryVar = null;
return;
}
this.qrCodeURL = window.location.href; // 赋值(这里的地址你自己定义)
let qrcode = new QRCode(this.$refs.qrcode, {
text: this.qrCodeURL, //页面地址 ,如果页面需要参数传递请注意哈希模式#
width: 100,
height: 100,
});
this.temporaryVar = qrcode;
},
},
};
</script>