QRCode
带有状态、图标、Canvas/SVG 和结果下载的二维码。
何时使用
QRCode 将文本转换为代码,以将链接或其他值发送到扫描仪设备。生成图片不控制链接过期、付款或授权:应用程序存储这些状态。
的画法意义及方法
value 接受字符串或段数组。 type 选择canvas 或svg,size 指定大小(以像素为单位)。空字符串不会创建 DOM,因此清除后 ref 会消失。
| 问题 | 设置 | 示例 |
|---|---|---|
| 创建代码 | value, type |
基本选项 |
| 添加小标志 | icon, iconSize |
图标 |
| 显示有效期 | status、onRefresh |
州 |
| 更换盖子 | statusRender |
你自己的封面 |
| 保存文件 | bind:nativeElement |
下载 |
| 比较校正 | errorLevel, boostLevel |
纠错 |
SVG完全在服务器上生成。画布在安装后绘制,并在水合之前保持空状态。如果您需要不带 JavaScript 的图像,请选择 SVG。切换和下载仍然需要客户端代码。
可读性、颜色和校正
color 设置模块的颜色,bgColor 设置背景。提供图像周围的对比度和清晰的 marginSize;它以模块为单位进行测量。更改 style.color 不会重新为模块着色。
errorLevel 支持 L/M/Q/H。如果图像版本保持不变,默认的 boostLevel 可以增加校正,因此更改 errorLevel 并不总是会改变图像。为了进行直观比较,该示例禁用了 boostLevel。
标志涵盖了设计的一部分。保持较小的尺寸,尤其是对于长文本。 iconSize 可以是数字或 width/height 对象。更高的密度需要增加 size 或减小值。 DOM 和图像检查不会取代扫描目标设备的摄像头。
状态、导出和注册
Active 显示常用代码,loading/过期/扫描添加封面图像。 onRefresh 报告刷新请求;新值和 status 由应用程序设置。 statusRender-snippet 获取非活动 status、locale 和回调。它会替换默认的封面内容,而不执行请求本身。
要导出,请通过 bind:nativeElement 获取root。 Canvas给出 toDataURL,SVG则通过XMLSerializer进行序列化。该示例使用数据 URL,因此不会创建临时对象 URL。 Canvas 的外部 icon 需要启用 CORS 的源。
ConfigProvider.qrcode 设置 class/style 和语义默认值,locale.qrcode — 状态签名。 Root/cover 由对象或回调配置; styles 包含 CSS 字符串。没有单独的公共 QRCode 组件设计令牌(design tokens)。通用主题影响框架和文本,并使用 color 或通用 colorText 设置模块的颜色。
在附近留下文本链接或代码用途说明:并非每个用户都可以扫描图像。向根传达一个可访问的名称,而无需仅将服务说明放在封面颜色中。
本地 prefixCls 更改 root、cover 和 QRCode 状态的 CSS 类;非空值优先。空字符串和 undefined 继承ConfigProvider.prefixCls,后缀为 -qrcode,默认为 ant-qrcode。 Data、canvas/SVG、onRefresh、nativeElement 和语义槽保留其契约。 theme.cssVar.prefix 分别指定 CSS 变量的名称。 前缀和状态 在标准 QRCode 旁边显示本地命名空间; ConfigProvider 是通用前缀。包装 CSS 时,请使用您选择的前缀或语义 classNames。
Spin 在 loading 状态下单独继承了provider的通用前缀,而不是本地的 QRCode.prefixCls。更改命名空间 QRCode 不会自行重命名嵌套组件。
示例
基本示例
地址线和各个段。有边框的画布和无边框的 SVG。
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
figure {
display: grid;
gap: 12px;
margin: 0;
}
figcaption { color: var(--ant-color-text-secondary); }
</style>
<div class="examples">
<figure>
<QRCode value="https://woodger.org/ant-design" marginSize={4}
aria-label={("图书馆主页二维码")} />
<figcaption>{("地址线")}</figcaption>
</figure>
<figure>
<QRCode value={['https://woodger.org/', 'ant-design']} type="svg" bordered={false}
marginSize={4} aria-label={("来自不同段的 QR 码")} />
<figcaption>{("分段,无框架")}</figcaption>
</figure>
</div>
代码中的图标
本地图标、方形和矩形 iconSize、H 校正。
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" icon="/favicon.png" iconSize={28}
errorLevel="H" marginSize={4} aria-label={("带图标的画布二维码")} />
<QRCode value="https://woodger.org/ant-design" type="svg" icon="/favicon.png"
iconSize={{ width: 32, height: 24 }} errorLevel="H" marginSize={4}
aria-label={("带有矩形图标的 SVG QR 代码")} />
</div>
二维码已过时
已扫描
四种状态
活动、loading、过期和已扫描。此次更新消除了过期代码的覆盖。
<script lang="ts">
import { QRCode, type QRStatus } from '@woodger/antd';
const states = [
{ value: 'active', label: "积极的" },
{ value: 'loading', label: "加载中" },
{ value: 'expired', label: "期限已过" },
{ value: 'scanned', label: "已扫描" }
] as const satisfies readonly { value: QRStatus; label: string }[];
let expired = $state(true);
let refreshed = $state(0);
function refresh() {
expired = false;
refreshed++;
}
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
figure {
display: grid;
gap: 12px;
margin: 0;
}
figcaption, output { color: var(--ant-color-text-secondary); }
output {
display: block;
margin-top: 20px;
}
</style>
<div class="examples">
{#each states as state (state.value)}
<figure>
<QRCode value="https://woodger.org/ant-design" type="svg" marginSize={4}
status={state.value === 'expired' && !expired ? 'active' : state.value}
onRefresh={refresh} aria-label={("二维码: " + String((state.label)) + "")} />
<figcaption>{(state.label)}</figcaption>
</figure>
{/each}
</div>
<output
aria-label={("二维码更新")}
>{("更新: ")}{refreshed}</output>
二维码已过时
自己的状态封面
statusRender 接收状态、区域设置和回调更新。
<script lang="ts">
import { Button, QRCode, RadioGroup, type QRStatus, type StatusRenderInfo } from '@woodger/antd';
let status = $state<QRStatus>('expired');
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.cover {
display: grid;
justify-items: center;
gap: 8px;
text-align: center;
}
p { margin: 0; }
</style>
{#snippet renderStatus(info: StatusRenderInfo)}
<div class="cover">
{#if info.status === 'expired'}
<p>{(info.locale.expired)}</p>
<Button size="small" onclick={info.onRefresh}>{("获取新代码")}</Button>
{:else if info.status === 'loading'}
<p role="status">{("我们正在准备新代码...")}</p>
{:else}
<p role="status">{("登录已确认")}</p>
{/if}
</div>
{/snippet}
<div class="example">
<RadioGroup aria-label={("自己的封面状况")} value={status}
options={(['active', 'expired', 'loading', 'scanned'])}
onValueChange={next => { status = next; }} />
<QRCode value="https://woodger.org/ant-design" type="svg" {status} marginSize={4}
statusRender={renderStatus} onRefresh={() => { status = 'active'; }}
aria-label={("自带封面的二维码")} />
</div>
画布或 SVG
切换一个值的输出方法。
<script lang="ts">
import { QRCode, RadioGroup } from '@woodger/antd';
let type = $state<'canvas' | 'svg'>('canvas');
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("二维码输出方式")} value={type} options={(['canvas', 'svg'])}
optionType="button" onValueChange={next => { type = next; }} />
<QRCode value="https://woodger.org/ant-design" {type} marginSize={4}
aria-label={("所选格式的二维码")} />
</div>
代码大小
从 48 到 300 像素;图标占据了四分之一的大小。
<script lang="ts">
import { Button, QRCode } from '@woodger/antd';
let size = $state(160);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.drawing {
max-width: 100%;
overflow-x: auto;
}
.drawing:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<div class="controls">
<Button aria-label={("缩小二维码")} disabled={size <= 48}
onclick={() => { size = Math.max(48, size - 10); }}>−</Button>
<Button aria-label={("放大二维码")} disabled={size >= 300}
onclick={() => { size = Math.min(300, size + 10); }}>+</Button>
<output aria-label={("二维码尺寸")}>{size}px</output>
</div>
<div class="drawing" role="region" aria-label={("二维码区")} tabindex="0">
<QRCode value="https://woodger.org/ant-design" {size} icon="/favicon.png"
iconSize={size / 4} errorLevel="H" marginSize={4} aria-label={("可变尺寸二维码")} />
</div>
</div>
颜色和背景
自己的模块颜色和背景具有额外的对比度。
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" color="#237804" bgColor="#f6ffed"
marginSize={4} aria-label={("绿色二维码")} />
<QRCode value="https://woodger.org/ant-design" type="svg" color="#9e1068" bgColor="#fff0f6"
marginSize={4} aria-label={("粉色二维码")} />
</div>
下载二维码
来自 Canvas 的 PNG 和通过 nativeElement 的 SVG,无需临时对象 URL。
<script lang="ts">
import { Button, QRCode, RadioGroup } from '@woodger/antd';
let type = $state<'canvas' | 'svg'>('canvas');
let nativeElement = $state<HTMLDivElement>();
function download() {
const drawing = nativeElement?.querySelector('canvas, svg');
if (!drawing) {
return;
}
const link = document.createElement('a');
link.download = `woodger-qrcode.${type === 'canvas' ? 'png' : 'svg'}`;
link.href = drawing instanceof HTMLCanvasElement
? drawing.toDataURL('image/png')
: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
new XMLSerializer().serializeToString(drawing)
)}`;
link.click();
}
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("二维码下载格式")} value={type} options={(['canvas', 'svg'])}
optionType="button" onValueChange={next => { type = next; }} />
<QRCode value="https://woodger.org/ant-design" {type} bind:nativeElement
color="#141414" bgColor="#ffffff" marginSize={4} aria-label={("二维码下载")} />
<Button type="primary" onclick={download}>{("下载 ")}{(type === 'canvas' ? 'PNG' : 'SVG')}</Button>
</div>
纠错
L/M/Q/H 级别并自动增加 boostLevel。
<script lang="ts">
import { Checkbox, QRCode, RadioGroup, type QRCodeErrorLevel } from '@woodger/antd';
let errorLevel = $state<QRCodeErrorLevel>('M');
let boostLevel = $state(false);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<RadioGroup aria-label={("二维码校正级别")} value={errorLevel}
options={(['L', 'M', 'Q', 'H'])} optionType="button"
onValueChange={next => { errorLevel = next; }} />
<Checkbox bind:checked={boostLevel}>{("自动增加修正")}</Checkbox>
<QRCode
value="https://woodger.org/ant-design/components/qr-code"
type="svg"
{errorLevel}
{boostLevel}
marginSize={4}
aria-label={("带纠错功能的二维码")}
/>
</div>
弹出框内的代码
该按钮会打开一个代码,将链接发送到您的手机。
<script lang="ts">
import { Button, Popover, QRCode } from '@woodger/antd';
</script>
{#snippet code()}
<QRCode value="https://woodger.org/ant-design" type="svg" color="#141414"
bgColor="#ffffff" marginSize={4} bordered={false} aria-label={("二维码发送链接")} />
{/snippet}
<Popover
title={("在手机上打开页面")}
content={(code)}
trigger="click"
>
<Button>{("分享链接")}</Button>
</Popover>
二维码已过时
已扫描
Semantic DOM
styles/classNames 对象和函数样式 root 和覆盖。
<script lang="ts">
import { QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" status="expired"
classNames={{ root: 'qrcode-semantic-object', cover: 'qrcode-object-cover' }}
styles={{
root: 'border-radius: 16px',
cover: 'backdrop-filter: blur(2px)'
}}
aria-label={("具有对象样式的 QR 码")} />
<QRCode value="https://woodger.org/ant-design" type="svg" status="scanned"
classNames={() => ({ root: 'qrcode-semantic-function' })}
styles={({ props }) => ({
root: 'border: 2px solid var(--ant-color-success)',
cover: `font-weight: ${props.status === 'scanned' ? 700 : 400}`
})} aria-label={("带有样式功能的二维码")} />
</div>
QR code expired
代码已过期
常规设置和区域设置
ConfigProvider.qrcode、本地样式和自定义状态签名。
<script lang="ts">
import { Button, ConfigProvider, QRCode, enUS, ruRU } from '@woodger/antd';
let englishExpired = $state(true);
let customExpired = $state(true);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.codes {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="example">
<div class="codes">
<ConfigProvider locale={enUS} qrcode={{ styles: { root: 'border-radius: 12px' } }}>
<QRCode value="https://woodger.org/ant-design" type="svg"
status={englishExpired ? 'expired' : 'active'}
onRefresh={() => { englishExpired = false; }} aria-label={("带有英文区域设置的二维码")} />
</ConfigProvider>
<ConfigProvider locale={({
...ruRU, qrcode: {
refresh: "换一个新的",
expired: "代码已过期"
}
})}
qrcode={{ styles: { root: 'border-radius: 12px' } }}>
<QRCode value="https://woodger.org/ant-design" type="svg"
status={customExpired ? 'expired' : 'active'} styles={{ root: 'border-radius: 20px' }}
onRefresh={() => { customExpired = false; }} aria-label={("带有您签名的二维码")} />
</ConfigProvider>
</div>
<Button onclick={() => {
englishExpired = true;
customExpired = true;
}}>{("再次显示过期代码")}</Button>
</div>
已扫描
图片主题和封面
模块的通用颜色、框架的本地别名和状态文本。
<script lang="ts">
import { ConfigProvider, QRCode } from '@woodger/antd';
</script>
<style>
.examples {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<ConfigProvider theme={{
token: { colorText: '#135200' },
components: {
QRCode: {
colorBgContainer: '#fff7e6',
colorSplit: '#d3adf7',
colorText: '#722ed1',
borderRadiusLG: 16
}
}
}}>
<div class="examples">
<QRCode value="https://woodger.org/ant-design" type="svg" marginSize={4}
aria-label={("本地主题二维码")} />
<QRCode value="https://woodger.org/ant-design" type="svg" status="scanned" marginSize={4}
aria-label={("扫描本地主题二维码")} />
</div>
</ConfigProvider>
编辑点评
过期代码的文本、格式、清理和更新。
<script lang="ts">
import { Button, Input, QRCode, RadioGroup, type RadioOption } from '@woodger/antd';
const formats: RadioOption<'svg' | 'canvas'>[] = [
{ value: 'svg', label: 'SVG' },
{ value: 'canvas', label: 'Canvas' }
];
let value = $state('https://woodger.org/ant-design');
let type = $state<'svg' | 'canvas'>('svg');
let expired = $state(false);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 16px;
max-width: 520px;
}
.field {
display: grid;
gap: 8px;
width: 100%;
}
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
}
p {
margin: 0;
color: var(--ant-color-text-secondary);
}
</style>
<div class="example">
<div class="field">
<label for="showcase-qrcode-value">{("二维码文字")}</label>
<Input id="showcase-qrcode-value" bind:value maxLength={200} allowClear />
</div>
<div class="controls">
<RadioGroup
aria-label={("二维码格式")}
options={(formats)}
value={type}
optionType="button"
onValueChange={next => { type = next; }}
/>
<Button disabled={!value} onclick={() => { expired = !expired; }}>{(expired ? "激活码" : "期限已过")}</Button>
</div>
{#if value}
<QRCode
{value}
{type}
size={192}
status={expired ? 'expired' : 'active'}
onRefresh={() => { expired = false; }}
aria-label={("页面二维码")}
/>
{:else}
<p>{("输入文本以创建 QR 码。")}</p>
{/if}
</div>
二维码已过时
本地 CSS 前缀
使用自定义 prefixCls 更新 SVG、状态和代码;标准邻居
<script lang="ts">
import { Checkbox, QRCode } from '@woodger/antd';
let alternate = $state(false);
let expired = $state(true);
</script>
<style>
.example {
display: grid;
gap: 24px;
}
.codes {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
</style>
<div class="example">
<Checkbox bind:checked={alternate}>{("另一个 QRCode 前缀")}</Checkbox>
<Checkbox bind:checked={expired}>{("带前缀的过期代码")}</Checkbox>
<div class="codes">
<QRCode prefixCls={alternate ? 'woodger-code' : 'woodger-qrcode'} type="svg"
value="https://woodger.org/ant-design" status={expired ? 'expired' : 'active'}
onRefresh={() => { expired = false; }} aria-label={("带前缀的二维码")} />
<QRCode type="svg" value="https://woodger.org/ant-design" aria-label={("标准二维码")} />
</div>
</div>
API
针对 11 个 ant.design 6.7.0 主题和基于 6.6.8 的公共 Svelte API 进行了验证。 statusRender 接受片段、styles - CSS 字符串。 React ref 被 bind:nativeElement 取代。 Props 的完整定义位于包中。
| 主题 | 例子 |
|---|---|
| base | 输入文本并更新图片。 |
| With Icon | Canvas 和 SVG 中的本地图标。 |
| other status | 四种状态和 onRefresh。 |
| custom status render | 包含 locale、status 和 onRefresh 的片段。 |
| Custom Render Type | 画布/SVG 切换。 |
| Custom Size | 代码大小和比例图标。 |
| Custom Color | 模块的颜色和不透明背景。 |
| Download QRCode | 通过 nativeElement 下载PNG 和SVG。 |
| Error Level | L/M/Q/H 和单独的 boostLevel。 |
| Advanced Usage | 弹出框内的 QRCode。 |
| Custom semantic dom styling | root 和 cover 的 styles/classNames 对象和函数。 |
| Local CSS prefix | 更改 CSS 类,同时保留 SVG、cover 和 onRefresh。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | root、cover 和 states 的完整本地 CSS 前缀。空字符串继承公共前缀; ant-qrcode 无需配置即可使用。数据、Canvas/SVG 和语义槽都被保留。 | string | ConfigProvider + -qrcode |
value | 文本或单独的片段。空行隐藏组件。 | string | string[] | 必需的 |
type | 挂载后绘制Canvas; SVG 可与 SSR 一起使用。 | 'canvas' | 'svg' | canvas |
size | CSS 像素大小;位图画布考虑了 devicePixelRatio。 | number | 160 |
icon | 图标网址;对于 Canvas,使用 crossOrigin=anonymous。 | string | — |
iconSize | 图标大小(以像素为单位)。 | number | { width: number; height: number } | 40 |
color | 模块的颜色。 style.color 和组件别名 colorText 不会替换它。 | string | 通用颜色文本 |
bgColor | 图片背景;在示例中,下载设置为白色。 | string | transparent |
marginSize | 模块周围的自由区;在主要示例中,它设置为 4。 | number | 0 |
bordered | false 删除可见边框、padding 和圆角。 | boolean | true |
errorLevel | 纠错级别。 | 'L' | 'M' | 'Q' | 'H' | M |
boostLevel | 如果不需要更大版本的 QR 码,则增加修正。 | boolean | true |
status | 托管封面状态。 | 'active' | 'expired' | 'loading' | 'scanned' | active |
onRefresh | 请求更新。新的 value/status 由所有者设置。 | () => void | — |
statusRender | 获取非活动 status、locale 和 onRefresh。 | Snippet<[StatusRenderInfo]> | 内置盖 |
classNames | root/cover 类的对象或函数。 | QRCodeSemantic | — |
styles | root/cover CSS 字符串对象或函数。 | QRCodeSemantic | — |
nativeElement | bind:nativeElement 挂载后提供 root;卸载时清除。 | HTMLDivElement | undefined | — |
ConfigProvider 和区域设置
ConfigProvider.qrcode 指定 class、styles 和语义 styles。局部 style 指定常规设置;正常 style 在语义 root 之后应用。嵌套提供程序替换整个 qrcode 对象。 locale.qrcode 结合了过期、刷新和扫描字段。
Semantic DOM
该容器包含带有 role=img 的画布或 svg。 aria-label 指的是绘图;给它一个可以解释代码用途的名称。图片本身不是链接。要打开该地址,请在应用程序中的附近留下可访问的文本链接。
| Slot | 目的 |
|---|---|
root | 容器、框架、背景、填充和尺寸。 |
cover | 覆盖非活动状态。 |
statusRender 替换了过期、loading 和扫描的封面内容。空的片段留下空的封面。当活动时,不会调用代码片段。 onRefresh 之后改变状态和更新数据属于这个例子。
注册令牌
没有单独的公共 QRCode 组件令牌。常见别名可以在 theme.components.QRCode 中重写:边框遵循 colorSplit,圆角遵循 borderRadiusLG,覆盖背景遵循 colorBgContainer。图片使用 color 或者通用的 theme.token.colorText;组件别名 colorText 更改状态文本。
| 代币 | 意义 |
|---|---|
colorSplit | rgba(5,5,5,0.06) |
colorText | rgba(0,0,0,0.88) |
colorWhite | #fff |
borderRadiusLG | 8 |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
lineType | solid |
lineWidth | 1 |
paddingSM | 12 |
FAQ
为什么更改 errorLevel 没有改变图片?
boostLevel 可以在不增加版本的情况下增加修正。为了比较,将其关闭。
如何下载 PNG 或 SVG?
从 nativeElement 获取canvas/svg,使用 toDataURL 或XMLSerializer。对于外部图标,请查看 CORS。
没有 JavaScript 可以做什么?
SVG是用SSR生成的;画布是在安装后绘制的。管理和下载需要水分。
为什么代码不可读?
检查对比度、空白、徽标尺寸和图案密度。然后检查目标设备的摄像头。