前端利用qrcodejs2封装二维码组件

该博客展示了如何在Vue组件中使用qrcodejs2库来生成二维码。代码中定义了一个名为QrCode的组件,该组件接收父组件传递的qrCodeUrl属性,并在页面加载后1秒生成相应内容的二维码。二维码的尺寸、颜色和错误校正级别均可定制,样式简洁,显示在一个带有边框的方块中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QrCode.vue

<template>
  <el-col :span="4" class="d-flex flex-column mb14">
    <p>二维码
      <span><sp-icon type="sp-question" fontSize="14"></sp-icon></span>
    </p>
    <div class="out-div">
      <div id="qrcode" ref="qrCodeUrl"></div>
    </div>
</el-col>
</template>

<script>
import QRCode from 'qrcodejs2'
export default {
  name: '',
  data () {
    return {}
  },
  created () {},
  mounted () {
    this.creatQrCode()
  },
  methods: {
    creatQrCode () {
      setTimeout(() => {
        // console.log(this.$attrs.qrCodeUrl)
        var qrcode = new QRCode(this.$refs.qrCodeUrl, {
          text: this.$attrs.qrCodeUrl,
          width: 90,
          height: 90,
          colorDark: '#69a775',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
      }, 1000)
    }
  }
}
</script>

<style scoped lang='stylus'>
.out-div
  width 100px
  height 100px
  border 2px solid #69a775
  display flex
  justify-content center
  align-items center
</style>

引入 因为url在祖组件中

<qr-code v-bind="$attrs" v-on="$listeners"></qr-code>
import QrCode from '@/pages/components/QrCode.vue'
components: {
    QrCode
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值