Vue 中 qrcode.vue 生成二维码以及添加中心logo

一、安装插件
npm i qrcode.vue -S
二、使用组件
<template>
  <van-popup
    v-model="showQr"
    v-bind="popupConfig"
  >
    <div class="qrcode-wrapper">
      <div class="qrcode-title">
        <van-icon name="scan" />
        <span>{{$t('appQrCode.title')}}</span>
      </div>
      <div class="qrcode-main">
        <qrcode-vue
          class="qrcode-code"
          level="H"
          :size="qrCodeSize"
          :value="getLocalUrl()"
        />
        <section class="logo shadowed">
          <BaseImage :src="logo" />
        </section>
      </div>
    </div>
  </van-popup>
</template>
<script>
import { Popup, Icon } from 'vant';
import QrcodeVue from 'qrcode.vue';

export default {
  name: 'AppQrCode',
  components: {
    [Popup.name]: Popup,
    [Icon.name]: Icon,
    QrcodeVue
  },

  props: {
    popupConfig: {
      type: Object,
      default: () => ({
        overlay: true,
        position: 'bottom',
        transitionAppear: true,
        safeAreaInsetBottom: true,
        closeable: true
      })
    },
    logo: {
      type: String,
      default: 'https://lark-assets-prod-aliyun.oss-cn-hangzhou.aliyuncs.com/yuque/0/2021/jpeg/1505703/1617878234987-resources/11316848/jpg/ba813e1a-b8ac-4d9a-a796-088a169bad43.jpeg?OSSAccessKeyId=LTAI4GGhPJmQ4HWCmhDAn4F5&Expires=1624251071&Signature=obJX0Ziien02rj%2BJ31Rc2QR3Wsg%3D'
    }
  },

  data() {
    return {
      qrCodeSize: document.body.offsetWidth / 2
    };
  },

  computed: {
    showQr: {
      get() {
        return this.$store.getters.showQr;
      },
      set(val) {
        this.$store.dispatch('app/toggleShowQr', val);
      }
    }
  },

  methods: {
    getLocalUrl() {
      return window.location.href;
    }
  }
};
</script>
样式代码:
.qrcode {
    &-wrapper {
        padding: 60px 35px;
        font-size: 14px;
        text-align: center;
    }

    &-title {
        margin-bottom: 30px;

        i {
            margin-right: 4px;
            color: @primary-color;
            font-size: 16px;
            vertical-align: text-bottom;
        }
    }

    &-main {
      position: relative;

      .logo {
        width: 1.1rem;
        height: 1.1rem;
        overflow: hidden;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: #fff;
        border: 2px solid #fff;
        border-radius: 6px;;
      }
      
	  .shadowed {
    	box-shadow: 0px 0px 3px 3px rgba(0, 0, 0, 0.1);
	  }

      .van-image {
        border-radius: 4px;
        overflow: hidden;
      }
    }
}
三、最终效果

在这里插入图片描述

qrcode.vue暂时不支持配置二维码中间logo,后续作者会完善功能,另有附带logo配置的vue-qr插件,但是包体积足足有4m+之大,不符合项目预期,所以暂时使用定位覆盖临时方案。
https://github.com/scopewu/qrcode.vue/issues/36

使用qrcode.vue生成二维码的步骤如下: 1. 引入qrcode.vue组件,该组件的体积相对较小,但不能添加中心logo。 2. 在Vue模板使用vue-qr组件,设置id为"qrcode",并传入生成二维码所需的text和size参数。 3. 在Vue模板添加一个按钮,当点击按钮时触发saveImg函数。 4. 在Vue的script setup,使用import语句引入qrcode.vue组件和其他所需的资源。 5. 根据需要,可以在logoSrc变量设置logo的路径。 6. 在saveImg函数,获取生成的二维码图片元素,创建一个下载链接,并设置链接的href和download属性。 7. 点击按钮后,调用a元素的click方法,实现下载二维码的功能。 你可以参考以下代码示例: ```html <template> <qrcode id="qrcode" :text="value" size="135"></qrcode> <br /> <el-button @click="saveImg">下载二维码</el-button> </template> <script setup> import qrcode from 'qrcode.vue' import code_logo from '../../assets/vue.svg' const props = defineProps({ value: { type: String, default: 'https://www.baidu.com/' } }) let logoSrc = code_logo function saveImg() { let picData = document.getElementById("qrcode") let a = document.createElement("a"); a.href = picData.src; a.download = "qrcode.png"; a.click(); } </script> ``` 请注意,以上代码只是一个示例,具体的实现方式可能会根据你的实际需求和项目配置而有所不同。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3项目生成并下载二维码,关于vue-qr.vueqrcode.vue插件之间的区别](https://blog.csdn.net/qq_51758070/article/details/128232266)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超喜欢你呦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值