Skip to content

BarCode 条形码 ​

用于生成一维条形码,基于 JsBarcode 实现,支持多种常见条码格式,可在 H5、小程序和 App 端使用。

组件类型 ​

基本用法 ​

通过 value 设置条形码内容,默认使用 auto 格式。

html
<wd-bar-code value="1234567890" />

常见格式 ​

通过 format 指定条码格式,例如 EAN13、CODE39、ITF14。

html
<wd-bar-code value="690123456789" format="EAN13" />
<wd-bar-code value="CODE39" format="CODE39" />
<wd-bar-code value="1234567890123" format="ITF14" />

组件变体 ​

文字位置 ​

通过 text-position 控制文字显示在条码上方或下方。

html
<wd-bar-code value="1234567890" text-position="top" />
<wd-bar-code value="1234567890" text-position="bottom" />

文字间距 ​

通过 text-margin 调整文字与条码之间的间距。

html
<wd-bar-code value="1234567890" :text-margin="10" />

隐藏文字 ​

通过 display-value 控制是否显示条码文本。

html
<wd-bar-code value="1234567890" :display-value="false" />

组件样式 ​

自定义颜色 ​

通过 line-color 和 background 分别设置线条颜色和背景色。

html
<wd-bar-code value="1234567890" line-color="#4D80F0" background="#E0EAFF" />

自定义尺寸 ​

通过 width 调整条形码整体宽度,通过 height 调整包含上下留白、条和文本在内的整体高度。

html
<!-- 指定整体宽度 -->
<wd-bar-code value="1234567890" :width="260" :height="80" />

<!-- 宽而矮的紧凑尺寸 -->
<wd-bar-code value="1234567890" :width="320" :height="50" />

<!-- 增加条形码整体高度 -->
<wd-bar-code value="1234567890" :height="140" />

<!-- 隐藏文字后做紧凑尺寸 -->
<wd-bar-code value="1234567890" :height="60" :display-value="false" />

字体样式 ​

通过 font-size 设置条码文本的最大字号,文本宽度超出可用空间时会自动缩小。通过 font-options 设置文字样式,可选值为 bold、italic、bold italic。

html
<wd-bar-code value="1234567890" :font-size="14" />
<wd-bar-code value="1234567890" :font-size="24" />
<wd-bar-code value="1234567890" font-options="bold italic" />

特殊样式 ​

支持格式 ​

组件支持 CODE128、CODE128A、CODE128B、CODE128C、EAN13、EAN8、UPC、UPCE、CODE39、ITF14、MSI、MSI10、MSI11、MSI1010、MSI1110、pharmacode、codabar 等格式。

html
<wd-bar-code value="690123456789" format="EAN13" />
<wd-bar-code value="A123456A" format="codabar" />

特殊格式尺寸 ​

EAN8、UPC、UPCE 这类格式也可以单独调整宽高。

html
<wd-bar-code value="1234567" format="EAN8" :width="210" :height="120" />
<wd-bar-code value="12345678901" format="UPC" :width="240" :height="130" />
<wd-bar-code value="123456" format="UPCE" :width="200" :height="110" />

导出图片 ​

通过组件实例调用 exportImage() 导出当前条形码图片。

vue
<template>
  <view>
    <wd-bar-code ref="barCodeRef" value="1234567890" />
    <wd-button icon="download" size="small" @click="handleExportImage">导出图片</wd-button>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { BarCodeInstance } from '@/uni_modules/wot-ui/components/wd-bar-code/types'

const barCodeRef = ref<BarCodeInstance | null>(null)

async function handleExportImage() {
  const path = await barCodeRef.value?.exportImage()
  console.log(path)
}
</script>

BarCode Attributes ​

参数说明类型默认值
value v2.0.5条形码内容string | number-
format v2.0.5条形码格式,可选值为 auto、CODE128、CODE128A、CODE128B、CODE128C、EAN13、EAN8、UPC、UPCE、CODE39、ITF14、MSI、MSI10、MSI11、MSI1010、MSI1110、pharmacode、codabarstringauto
width v2.0.5条形码整体宽度,必须为有限正数number200
height v2.0.5条形码整体渲染高度,包含上下留白、条和文本number100
text v2.0.5自定义显示文本,默认使用编码器生成的文本,包含自动补充的校验位string''
font v2.0.5字体stringmonospace
font-size v2.0.5文本最大字号,宽度不足时自动缩小number20
font-options v2.0.5字体样式,可选值为 bold、italic、bold italicstring''
text-margin v2.0.5文本与条码的间距,必须为有限非负数number2
background v2.0.5背景色string#ffffff
line-color v2.0.5线条颜色string#000000
margin v2.0.5边距,必须为有限非负数number10
margin-top v2.0.5上边距,必须为有限非负数,未设置时使用 marginnumber-
margin-bottom v2.0.5下边距,必须为有限非负数,未设置时使用 marginnumber-
margin-left v2.0.5左边距,必须为有限非负数,未设置时使用 marginnumber-
margin-right v2.0.5右边距,必须为有限非负数,未设置时使用 marginnumber-
display-value v2.0.5是否显示文本booleantrue
text-align v2.0.5文本对齐方式,可选值为 left、center、right;适用于普通格式及自定义文本,EAN13、EAN8、UPC、UPCE 的默认数字保持分段居中stringcenter
text-position v2.0.5文本位置,可选值为 bottom、topstringbottom

BarCode Events ​

事件名称说明参数
error v2.0.5生成失败时触发error
valid v2.0.5每次编码校验完成时触发,即使结果未变化;仅表示编码内容是否合法,不代表绘制或导出成功valid: boolean

BarCode Methods ​

方法名说明参数返回值
exportImage v2.0.5导出条形码图片-Promise<string>

外部样式类 ​

类名说明
custom-class根节点样式类

源代码 ​

文档
• 组件

Released under the MIT License.

Released under the MIT License.