RBarcode
RBarcode 使用 JsBarcode 将字符串绘成条码。二维码使用二维码组件。
交互示例
编辑文本或切换制式,观察条码和校验提示。示例直接加载模板的 RBarcode。
查看示例源码
vue
<template>
<div class="ray-demo" :lang="locale ?? 'zh-CN'">
<div class="ray-demo-fields">
<div>
<label :for="textId">{{ copy.text }}</label>
<input
:id="textId"
v-model="text"
class="ray-demo-input"
maxlength="24"
:inputmode="format === 'EAN13' ? 'numeric' : 'text'"
:aria-describedby="hintId"
:aria-invalid="Boolean(text && failed)"
/>
</div>
<fieldset>
<legend>{{ copy.format }}</legend>
<div class="ray-demo-options ray-demo-native-options">
<label
v-for="item in formats"
:key="item"
:for="`${formatName}-${item}`"
>
<input
:id="`${formatName}-${item}`"
v-model="format"
type="radio"
:name="formatName"
:value="item"
/>
{{ item }}
</label>
</div>
</fieldset>
</div>
<p :id="hintId" class="ray-demo-hint" aria-live="polite">
{{ !text ? copy.empty : failed ? copy.invalid : '' }}
{{ format === 'EAN13' ? copy.ean : '' }}
</p>
<div
v-if="text"
v-show="!failed"
class="ray-demo-barcode"
role="img"
:aria-label="`${format}: ${text}`"
>
<RBarcode
:text="text"
:format="format"
:options="options"
barcodeRender="svg"
@success="failed = false"
@error="failed = true"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { RBarcode } from '@ray-template/ui/barcode'
import { computed, ref, useId } from 'vue'
const props = defineProps<{ locale?: 'zh-CN' | 'en' }>()
const copy = computed(() =>
props.locale === 'en'
? {
text: 'Barcode text',
format: 'Format',
empty: 'Enter text to generate a barcode.',
invalid: 'This text is invalid for the selected format.',
ean: 'Try 590123412345: EAN-13 takes 12 digits or 13 with a valid check digit.',
}
: {
text: '条码文本',
format: '条码制式',
empty: '输入文本后生成条码。',
invalid: '这段文本不符合当前制式。',
ean: '可试试 590123412345:EAN-13 接受 12 位数字,或附带正确校验位的 13 位数字。',
},
)
const textId = useId()
const hintId = useId()
const formatName = useId()
const formats = ['CODE128', 'CODE39', 'EAN13'] as const
const format = ref<(typeof formats)[number]>('CODE128')
const text = ref('RAY-2026')
const failed = ref(false)
const options = {
width: 1.5,
height: 52,
background: '#fff',
lineColor: '#111',
}
</script>用法
tsx
import { RBarcode } from '@/components'
export const renderOrderBarcode = (orderNo: string) => (
<RBarcode
text={orderNo}
format="CODE128"
barcodeRender="svg"
options={{ displayValue: true, height: 60 }}
onError={(error) => console.error('条码格式无效', error)}
/>
)| 属性 | 默认 | 说明 |
|---|---|---|
text | 无 | 条码文本。留空或传入不符合格式的值时可能渲染失败。 |
format | 'CODE128' | 条码制式,覆盖 options.format。 |
barcodeRender | 'canvas' | canvas 或 svg。 |
options | {} | JsBarcode options,如 displayValue、lineColor、margin、条宽和条高。 |
width / height | 'auto' | 显示尺寸,数字按像素。与 options 中条码自身的条宽、条高不同。 |
responsive | false | 监听容器尺寸;用 width="responsive" 或 height="responsive" 让对应尺寸随容器调整。 |
watchText | true | 文本变化时重绘。format、options(包括响应式嵌套字段)、尺寸和渲染介质变化也会自动重绘,无需更换 key。 |
loading | false | 控制 Spin;调用方负责开关。 |
onSuccess(text, format, options)、onError(error)、onFinally() 支持函数或回调数组。成功回调收到原始 options;组件响应式调整的条宽和条高不写回该对象。绘制异常通过 onError 通知,业务代码可据此提示或清理旧预览。
支持 CODE39、CODE128 及 A/B/C 变体、EAN13/EAN8/EAN5/EAN2、UPC、ITF14/ITF、MSI/MSI10/MSI11/MSI1010/MSI1110、pharmacode、codabar。使用 EAN 或 UPC 前验证输入长度和校验位。打印后是否可扫取决于设备和成品尺寸。