外观
Barcode 条形码
约 466 字大约 2 分钟
2025-10-31
基于 JsBarcode 生成编码并通过画布绘制的条形码组件,支持多种条码格式、自定义文本样式与图片模式。
基本使用
value 为条形码内容,format 为条码格式,默认 CODE128,文本显示在条纹下方:
<un-barcode value="123456789012" :width="2" :height="70"></un-barcode>不同格式
format 可切换条码类型,如 CODE39、EAN13 等,内容需符合对应格式的字符要求:
<un-barcode value="ABC-123" format="CODE39" :width="1.5" :height="60"></un-barcode>自定义样式
color、background-color、text-position、text-align、font-size、text-margin 可调整条纹颜色、背景与文本样式;display-value 设为 false 时不显示文本:
<un-barcode
value="UVIEW-UNIX"
format="CODE39"
:width="1.5"
:height="60"
color="#1a9bff"
background-color="#eef5ff"
text-position="top"
text-align="left"
:font-size="14"
:text-margin="6"
></un-barcode>image 模式
mode="image" 时把画布结果导出为图片节点,便于长按保存:
<un-barcode value="20261127001" mode="image" :height="70"></un-barcode>API
Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 条形码内容 | String | - |
| format | 条形码格式,如 CODE128 / CODE39 / EAN13 | String | CODE128 |
| width | 单根条纹宽度,单位 px | String | Number | 2 |
| height | 条纹高度,单位 px | String | Number | 80 |
| color | 条纹与文本颜色 | String | #000000 |
| backgroundColor | 背景颜色,transparent 为透明 | String | transparent |
| displayValue | 是否显示文本 | Boolean | true |
| textPosition | 文本位置:top / bottom | String | bottom |
| fontOptions | 字体样式,如 bold / italic | String | - |
| font | 字体 | String | monospace |
| textAlign | 文本对齐:left / center / right | String | center |
| textMargin | 文本边距,单位 px | String | Number | 2 |
| fontSize | 文本字号 | String | Number | 20 |
| mode | 显示模式:canvas / image | String | canvas |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| change | 条形码绘制完成后触发 | - |
| error | 编码或导出失败时触发 | message:错误信息 |
Methods
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| refresh | 重新编码并绘制 | - | - |
| toTempFilePath | 导出条形码图片 | options | Promise<String> |
