Skip to content

Qrcode QR Code Component

Generates and displays a QR code on the front end using JS based on the provided string

📌Platform Differences

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

⚠️Notes

Notes

  • The QR code is generated by front-end JS computation, so there will be a computation time
  • If there are multiple QR code components on a page, you must set different cid properties for each
  • The icon property supports image URLs, but it is recommended to use smaller-sized images to ensure the QR code remains scannable
  • The error correction level lv ranges from 1 to 4; the larger the number, the stronger the error correction capability

🏯Basic Usage Examples

Basic Usage

html
<template>
    <hy-qrcode text="https://gxh151.top"></hy-qrcode>
</template>

Custom Size

html
<template>
    <hy-qrcode text="https://gxh151.top" :size="300"></hy-qrcode>
</template>

Custom Colors

html
<template>
    <hy-qrcode
        text="https://gxh151.top"
        background="#f5f5f5"
        foreground="#214283"
        pdGround="#214283"
    ></hy-qrcode>
</template>

Add a Center Icon

html
<template>
    <hy-qrcode
        text="https://gxh151.top"
        icon="https://pic1.imgdb.cn/item/67628833d0e0a243d4e5d22b.webp"
        :iconSize="50"
    ></hy-qrcode>
</template>

Custom Loading State

html
<template>
    <hy-qrcode text="https://gxh151.top" :showLoading="true" loadingText="Generating..."></hy-qrcode>
</template>

Disable Loading State

html
<template>
    <hy-qrcode text="https://gxh151.top" :showLoading="false"></hy-qrcode>
</template>

Multiple QR Codes

html
<template>
    <view class="qrcode-list">
        <hy-qrcode cid="qrcode1" text="https://example.com/1"></hy-qrcode>
        <hy-qrcode cid="qrcode2" text="https://example.com/2"></hy-qrcode>
        <hy-qrcode cid="qrcode3" text="https://example.com/3"></hy-qrcode>
    </view>
</template>

<style lang="scss" scoped>
    .qrcode-list {
        display: flex;
        flex-direction: column;
        gap: 20rpx;
    }
</style>

Enable Preview

html
<template>
    <hy-qrcode text="https://gxh151.top" :allowPreview="true" @preview="onPreview"></hy-qrcode>
</template>

<script setup>
    const onPreview = (url, event) => {
        console.log('Preview image:', url);
    };
</script>

Listen to Generation Events

html
<template>
    <hy-qrcode text="https://gxh151.top" @result="onResult" @longPress="onLongPress"></hy-qrcode>
</template>

<script setup>
    const onResult = (res) => {
        console.log('QR code generated successfully:', res);
    };

    const onLongPress = () => {
        console.log('QR code long pressed');
    };
</script>

API

Qrcode Props

ParameterDescriptionTypeDefault Value
cidInstance ID string (if there are multiple QR code components, each must have a unique cid)string'hy-qrcode-canvas_' + random(1, 1000)
sizeQR code size, default unit is pxnumber200
textQR code contentstring-
showWhether to display the QR codebooleantrue
backgroundQR code background colorstring#ffffff
foregroundQR code colorstring#000000
pdGroundPositioning corner colorstring#000000
lvError correction level (1-4)number3
usingComponentsWhether it is a custom componentbooleantrue
iconQR code center icon (image URL)string-
iconSizeQR code center icon size (px)number40
showLoadingWhether to show loading statebooleantrue
loadingTextLoading prompt textstringGenerating QR code
allowPreviewWhether to allow image previewbooleanfalse

Events

Event NameDescriptionCallback Parameters
resultQR code generated successfullyres: any data returned on success
previewOpen image eventurl: string image URL, event: Event event object
longPressQR code long press event-
03:29