Skip to content

QRCode 二维码

用于生成二维码的组件。

基础用法

最简单的二维码用法。

vue
<template>
  <ExQRCode value="https://github.com/vuejs/vue" />
</template>

不同尺寸

通过 size 属性可以设置二维码的大小。

vue
<template>
  <ExQRCode :value="url" :size="100" />
  <ExQRCode :value="url" :size="150" />
  <ExQRCode :value="url" :size="200" />
  <ExQRCode :value="url" :size="250" />
</template>

<script setup>
import { ref } from 'vue'
const url = ref('https://github.com/vuejs/vue')
</script>

不同类型

通过 type 属性可以设置不同的边框样式。

Default

Primary

Success

Warning

Danger

vue
<template>
  <ExQRCode :value="url" type="primary" />
  <ExQRCode :value="url" type="success" />
  <ExQRCode :value="url" type="warning" />
  <ExQRCode :value="url" type="danger" />
</template>

自定义颜色

通过 fgColorbgColor 属性可以自定义二维码的颜色。

霓虹蓝

霓虹粉

绿色

vue
<template>
  <ExQRCode 
    :value="url" 
    fg-color="#00f0ff"
    bg-color="#0a0a0a"
  />
</template>

带中心图标

通过 icon 插槽可以在二维码中心添加图标或 Logo。

游戏房间

玩家资料

下载游戏

vue
<template>
  <ExQRCode :value="gameUrl" type="primary" :icon-size="50">
    <template #icon>
      <img 
        src="https://api.iconify.design/ri/gamepad-line.svg" 
        alt="游戏" 
        style="width: 100%; height: 100%;" 
      />
    </template>
  </ExQRCode>
</template>

<script setup>
import { ref } from 'vue'
const gameUrl = ref('https://game.example.com/join?room=12345')
</script>

错误纠正级别

通过 level 属性可以设置错误纠正级别。级别越高,容错能力越强,但二维码越复杂。

L (7%)

M (15%)

Q (25%)

H (30%)

vue
<template>
  <ExQRCode :value="url" level="L" />
  <ExQRCode :value="url" level="M" />
  <ExQRCode :value="url" level="Q" />
  <ExQRCode :value="url" level="H" />
</template>

无边框

设置 borderedfalse 可以移除边框。

vue
<template>
  <ExQRCode :value="url" :bordered="false" />
</template>

自定义圆角

通过 border-radius 属性可以自定义圆角大小。

vue
<template>
  <ExQRCode :value="url" :border-radius="0" />
  <ExQRCode :value="url" :border-radius="16" />
  <ExQRCode :value="url" :border-radius="100" />
</template>

下载二维码

可以通过组件暴露的 download 方法下载二维码。

vue
<template>
  <ExQRCode ref="qrcodeRef" :value="url" type="primary" />
  <ExButton type="primary" @click="handleDownload">
    下载二维码
  </ExButton>
</template>

<script setup>
import { ref } from 'vue'

const url = ref('https://github.com/vuejs/vue')
const qrcodeRef = ref()

const handleDownload = () => {
  qrcodeRef.value?.download('my-qrcode.png')
}
</script>

API

QRCode Props

属性说明类型默认值
value二维码内容string-
size二维码大小number200
level错误纠正级别'L' | 'M' | 'Q' | 'H''M'
bg-color背景颜色string'#ffffff'
fg-color前景颜色string'#000000'
include-margin是否包含边距booleantrue
bordered是否显示边框booleantrue
border-radius边框圆角number8
icon-size中心图标大小number40
type二维码类型'default' | 'primary' | 'success' | 'warning' | 'danger''default'
error-text错误提示文本stringundefined
aria-label无障碍标签stringundefined

QRCode Slots

插槽名说明
icon中心图标/Logo

QRCode Methods

方法名说明类型
getElement获取二维码DOM元素() => HTMLDivElement | null
download下载二维码(filename?: string) => void
toDataURL获取二维码数据URL(type?: 'image/png' | 'image/jpeg') => string
refresh刷新二维码() => void

错误纠正级别说明

级别容错率说明
L7%低容错,适合内容简单的二维码
M15%中等容错,默认级别
Q25%较高容错,适合需要添加图标的二维码
H30%高容错,适合复杂环境或需要大图标

无障碍支持

  • 使用 role="img" 标记
  • 支持 aria-label 属性
  • 提供错误状态提示

注意事项

  1. 二维码内容越长,生成的二维码越复杂
  2. 添加中心图标会降低扫描成功率,建议使用较高的错误纠正级别(Q 或 H)
  3. 自定义颜色时,确保前景色和背景色有足够的对比度
  4. 二维码大小建议不小于 100px,以确保扫描成功率