Barcode 条形码
条形码组件(web 端 SVG 形态):文本经 @lh-ui/share/core/barcode 的 自研纯 JS 编码器生成模块位数组(encodeBarcode),再由 barcodeToSvgPath 转成条模块 path 串渲染进 <svg>(对齐 qrcode 组件 形态)。编码器为自研纯 JS,零第三方依赖:CODE128(A/B/C 子集自动 择优 + mod 103 校验位)与 EAN-13(12 位补校验位 / 13 位校验)。
支持条高、静区、前景 / 背景色与可读数字;空 value 与不可编码输入 (CODE128 非 ASCII、EAN13 校验位不匹配)降级为空态(不渲染 svg, console.warn 记录),绝不让编码错误冒泡到渲染层。
基础用法
value 为编码内容;type 选择制式(默认 CODE128):
基础用法
LH-UI-20266901234567892
vue
<script setup lang="ts">
import { LhBarcode } from '@lh-ui/vue3'
</script>
<template>
<!-- 基础用法 -->
<LhBarcode value="LH-UI-2026" show-text />
<LhBarcode value="690123456789" type="EAN13" show-text />
</template>vue
<script>
import { LhBarcode } from '@lh-ui/vue3'
export default {
components: { LhBarcode },
}
</script>
<template>
<!-- 基础用法 -->
<LhBarcode value="LH-UI-2026" show-text />
<LhBarcode value="690123456789" type="EAN13" show-text />
</template>EAN-13:12 位输入自动补校验位(如 690123456789 → 6901234567892), 13 位输入校验不通过抛错并降级空态。
高度与静区
height 为条高(默认 64px),margin 为左右静区模块数(默认 10, 扫码识别的下限);showText 在条码下方渲染等宽字体可读数字:
高度与静区
ABC-12345ABC-12345
vue
<script setup lang="ts">
import { LhBarcode } from '@lh-ui/vue3'
</script>
<template>
<!-- 高度与静区 -->
<LhBarcode value="ABC-12345" :height="48" :margin="4" show-text />
<LhBarcode value="ABC-12345" :height="80" :margin="16" show-text />
</template>vue
<script>
import { LhBarcode } from '@lh-ui/vue3'
export default {
components: { LhBarcode },
}
</script>
<template>
<!-- 高度与静区 -->
<LhBarcode value="ABC-12345" :height="48" :margin="4" show-text />
<LhBarcode value="ABC-12345" :height="80" :margin="16" show-text />
</template>自定义颜色
color 覆盖条模块前景色(未传走 --lh-text-primary 令牌), bgColor 绘制背景 rect(未传透明、走样式表令牌):
颜色
8866-20268866-2026
vue
<script setup lang="ts">
import { LhBarcode } from '@lh-ui/vue3'
</script>
<template>
<!-- 颜色 -->
<LhBarcode value="8866-2026" color="#2B4C7C" show-text />
<LhBarcode value="8866-2026" color="#D9393B" bg-color="#FFF7E8" show-text />
</template>vue
<script>
import { LhBarcode } from '@lh-ui/vue3'
export default {
components: { LhBarcode },
}
</script>
<template>
<!-- 颜色 -->
<LhBarcode value="8866-2026" color="#2B4C7C" show-text />
<LhBarcode value="8866-2026" color="#D9393B" bg-color="#FFF7E8" show-text />
</template>API
Barcode Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 编码内容(EAN13 为 12 或 13 位数字) | string | '' |
| type | 编码制式 | 'CODE128' | 'EAN13' | 'CODE128' |
| height | 条高(px) | number | 64 |
| margin | 左右静区(模块数) | number | 10 |
| color | 前景色(条) | string | — |
| bgColor | 背景色(空白) | string | — |
| showText | 底部可读数字 | boolean | false |
| label | 无障碍可达名称(默认「条形码」) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
无障碍
- 根节点为
role="img",可达名称默认「条形码」,可由label覆盖 - 空态(空
value/ 不可编码降级)不渲染 svg,DOM 中无残留节点 - 编码失败时
console.warn记录并降级为空态,不抛错
编码器为自研纯 JS(CODE128 / EAN-13 标准数据),零第三方依赖。

