Skip to content

Qrcode 二维码 ​

二维码组件:canvas 2d 形态绘制(share core 自研纯 JS 编码器 encodeQrcode

  • canvas 收口 drawQrcodeToCanvas,web 与小程序同构),无第三方依赖。 服务网址跳转、凭证展示、资产标签等场景。

平台差异(登记):本端为 canvas 2d 形态——小程序模板不支持 SVG (share qrcode styles 的 SVG 形态面向 web 端),本端用 <canvas type="2d"> 消费同一份 share core 编码与绘制纯函数;编码(UTF-8 字节模式、 Reed-Solomon 纠错、掩码择优、版本 1-40 全支持)全部复用 share core, 端内只做 canvas 取上下文与重绘调度。空 value 或超容量文本降级空态 (清空画布 + 控制台告警,视图层不抛错)。组件无事件、无插槽、纯受控。

基础用法 ​

vue
<template>
  <lh-qrcode value="https://www.lhgb.top" />
</template>

尺寸与纠错级别 ​

size 为 canvas 画布边长(px,默认 120);level 为纠错级别,级别越高 可容纳纠错容量越大、Damaged 可恢复率越高:

vue
<lh-qrcode value="lh-ui" :size="80" />
<lh-qrcode value="lh-ui" :size="160" level="H" />

指定版本与静默区 ​

version 为 1-40(0 = 自动选最小可容纳版本);margin 为静默区模块数 (默认 4,spec 最小值):

vue
<lh-qrcode value="lh-ui" :version="5" />
<lh-qrcode value="lh-ui" :margin="1" />
<lh-qrcode value="lh-ui" :version="2" :margin="0" />

自定义颜色 ​

color 为模块前景色,bgColor 为背景色(未传取令牌兜底色, canvas 不能消费 var(--lh-*) 字符串):

vue
<lh-qrcode value="https://www.lhgb.top" color="#B87800" bg-color="#F5F3EE" />
<lh-qrcode value="https://www.lhgb.top" color="#FFFFFF" bg-color="#2B4C7C" />

中文与长文本 ​

编码采用 UTF-8 字节模式,支持中文;超容量(v40-H 上限 1273 字节)时 组件 try/catch 守卫降级空态:

vue
<lh-qrcode value=" lh-ui · 琉璃瓦组件库 " />

Props ​

属性说明类型默认值
value编码文本(UTF-8,支持中文;空字符串为空态)string''
level纠错级别'L' | 'M' | 'Q' | 'H''M'
version版本 1-40(0 = 自动选最小可容纳版本)number0
margin静默区模块数(spec 最小值 4)number4
color前景色(模块色)string正文色兜底
bgColor背景色string背景色兜底
size尺寸 px(canvas 画布宽高)number120
customClass自定义类名(透传到根节点)string—
sxsx 快速布局属性LhSxProps—

无障碍 ​

根节点携带 role="img" 语义,aria-label 默认取编码文本(空值时回退 通用名称「二维码」),读屏软件可直接播报内容。