qrcode(vue3)

https://qrcode.nodejs.cn/docs/#qr-code-options  官网

1.npm install qrcode

2.components/code.vue

<script lang="ts" setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import QRCode from 'qrcode'

const props = defineProps({
  text: {
    type: String,
    default: ''
  },
  width: {
    type: Number,
    default: 200
  },
  height: {
    type: Number,
    default: 200
  },
  colorDark: {
    type: String,
    default: '#000000'
  },
  colorLight: {
    type: String,
    default: '#ffffff'
  }
})

const qrCodeRef = ref<HTMLCanvasElement | null>(null)

const drawQRCode = async () => {
  if (qrCodeRef.value && props.text) {
    await QRCode.toCanvas(qrCodeRef.value, props.text, {
      width: props.width,
      color: {
        dark: props.colorDark,
        light: props.colorLight
      }
    })
  }
}

onMounted(() => {
  nextTick(() => {
    drawQRCode()
  })
})

watch(() => props.text, () => {
  nextTick(() => {
    drawQRCode()
  })
})

watch([() => props.width, () => props.height, () => props.colorDark, () => props.colorLight], () => {
  nextTick(() => {
    drawQRCode()
  })
})
</script>

<template>
  <canvas ref="qrCodeRef" :width="width" :height="height" class="qrcode-container"></canvas>
</template>

<style scoped>
.qrcode-container {
  display: inline-block;
}
</style>

如果import QRCode from 'qrcode'报错

那就新建一个qrcodejs2.d.ts(和index.html同级)

declare module 'qrcode';

3.在页面引用

import QRCode from '@/components/code/code.vue'

// 二维码内容
const qrCodeText = ref('http://192.168.1.114:8888/mobile/upload')


<QRCode :text="qrCodeText" :width="80" :height="80" />

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值