思路:
-
点击分享按钮打开弹框
-
获取当前url
-
根据url地址生成二维码图片并插入到弹框
问题:
-
如何获取url
获取完整的url地址 location.href
- 拿到主机 location.host
- 拿到hash location.hash
- 拿到?参数 locatoin.search
- 如何根据url生成二维码
1、
安装生成二维码的插件 npm i qrcode ```
-
生成二维码完整代码如下:
<template> <div> <h2>详情页</h2> <button @click="shareFn">分享</button> <!-- 分享弹框 --> <van-popup v-model:show="show"> <img :src="imgURL" alt="二维码" /> </van-popup> </div> </template> <script setup> import { ref } from "vue"; import QRcode from "qrcode"; const show = ref(false); const imgURL = ref(""); //分享课程 const shareFn = () => { //获取当前课程的url const url = location.href QRcode.toDataURL(url, (err, res) => { if (err) return false; console.log("二维码:", res); imgURL.value = res; }); show.value = true; }; </script> <style lang="scss" scoped></style>