vue qrcode生成二维码 并拼装url参数

40 篇文章 3 订阅

因笔者需要用到动态二维码绑定关系这一项,故参考网上许多资料最终整理如下文档

 

第一步:

npm i qrcode --save

第二步:创建一个vue文件,用于放置你的二维码

<template>
    <div>
        
        <div>
            <canvas id="canvas"></canvas>
        </div>
    </div>
</template>

<script>
import QRCode from 'qrcode'
export default {
    data() {
        return {
            userinfo:{}
        }
    },
    created() {
        //这个是从store中获取的,因为笔者在登录的时候已经存入进去了
        this.userinfo = this.$store.state.userinfo;
    },
    mounted() {
        document.querySelector('body').setAttribute('style', 'background-color:#E9EAEE');
        this.useqrcode();
    },
    beforeDestroy() {
        document.querySelector('body').removeAttribute('style');
    },
    methods: {
        onClickLeft() {
            this.$router.go(-1);
        },

        useqrcode() {
            var canvas = document.getElementById('canvas');

            //域名例:https://www.baidu.com,拼装url参数
            //{ margin: 1 }设置二维码的白边为1
            //this.userinfo.user_id--是自己的唯一ID

            var url = '你的域名' + "?pid_id=" + this.userinfo.user_id;

            QRCode.toCanvas(canvas, url, { margin: 1 }, function (error) {
                if (error) console.error(error)
                console.log('QRCode success!');
            })
        },
    }
}
</script>

<style scoped>
</style>

最后:大功告成

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
没问题,下面是使用 vue-qrcode插件 生成二维码并下载的代码示例: 1.首先,你需要安装 vue-qrcode 插件,可以使用 npm 命令来进行安装: ``` npm install vue-qrcode --save ``` 2.然后,在需要使用二维码的 Vue 组件中,导入 vue-qrcode 插件并创建一个新的 Vue 实例,并在这个实例中传递需要生成的二维码数据和相关参数: ``` <template> <div> <qrcode-vue :value="qrData" :size="150"></qrcode-vue> <button @click="downloadQRCode()">下载二维码</button> </div> </template> <script> import QRCodeVue from 'vue-qrcode' import domtoimage from 'dom-to-image' export default { name: 'QRCodeGenerator', components: { QRCodeVue, }, data() { return { qrData: '二维码数据', } }, methods: { downloadQRCode() { const qrCodeEl = document.querySelector('.qrcode-vue') domtoimage .toBlob(qrCodeEl) .then(blob => { const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = 'qrcode.png' a.click() }) .catch(error => { console.error('Error occurred while downloading QR Code:', error) }) }, }, } </script> ``` 3.接下来,在 downloadQRCode 方法中,我们使用 dom-to-image 库将二维码图片转换为 Blob对象, 然后创建一个链接和 a 标签来生成下载链接,并触发 a 标签的 click事件进行下载。 请注意,这个例子使用了 dom-to-image库来转换图片为 Blob 对象,因此需要使用 npm 命令进行安装: ``` npm install dom-to-image --save ``` 希望这个代码示例能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余温无痕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值