Barcode 條碼
將 ASCII 內容或商品號碼生成為一維條碼,使用 SVG 顯示,支援匯出 SVG、PNG、JPEG、WebP。
引入
import { HuiBarcode } from '@nscode/handy-ui'
import type { BarcodeExpose, BarcodeFormat, BarcodeStatus } from '@nscode/handy-ui'
示例
基礎用法
<script setup lang="ts">
import { HuiBarcode } from '@nscode/handy-ui'
</script>
<template>
<HuiBarcode value="NS2026111800001" />
</template>
七種格式
CODE128 支援 ASCII 0–127;CODE39 支援大寫字母、數字及部分標點;其餘格式只支援數字。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
const items = [
{ format: 'CODE128', value: 'Hello-HandyUI' },
{ format: 'EAN13', value: '690123456789' },
{ format: 'EAN8', value: '9638507' },
{ format: 'UPC', value: '03600029145' },
{ format: 'CODE39', value: 'NSUI-2026' },
{ format: 'ITF14', value: '1540014128876' },
{ format: 'ITF', value: '123456' }
] as const
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace v-for="item in items" :key="item.format" direction="vertical">
<span>{{ item.format }}</span>
<HuiBarcode :format="item.format" :value="item.value" />
</HuiSpace>
</HuiSpace>
</template>
商品條碼與檢查碼
EAN-13 接受 12 位或 13 位,EAN-8 接受 7 位或 8 位,UPC-A 接受 11 位或 12 位,ITF-14 接受 13 位或 14 位。少一位時自動補上 GS1 檢查碼;位數完整時核對末位。EAN、UPC 的文字按商品碼分組,護線延伸到文字區。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace direction="vertical">
<span>12 位:自動補為 6901234567892</span>
<HuiBarcode value="690123456789" format="EAN13" />
</HuiSpace>
<HuiSpace direction="vertical">
<span>13 位:核對檢查碼</span>
<HuiBarcode value="6901234567892" format="EAN13" />
</HuiSpace>
<HuiSpace direction="vertical">
<span>末位錯誤</span>
<HuiBarcode value="6901234567891" format="EAN13" />
</HuiSpace>
</HuiSpace>
</template>
條寬與條高
bar-width 是最窄模組的寬度,整張圖寬度隨內容和格式變化;height 只控制條高,不含留白與文字。
EAN、UPC 的分組文字字級不超過 bar-width 的 10 倍。條寬為 1 時,預設字級從 14 收小為 10,避免數字壓到護線。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="end">
<HuiSpace v-for="width in [1, 2, 3]" :key="width" direction="vertical">
<span>bar-width={{ width }}</span>
<HuiBarcode value="NS2026" :bar-width="width" />
</HuiSpace>
<HuiSpace direction="vertical">
<span>EAN-13 / bar-width=1</span>
<HuiBarcode value="690123456789" format="EAN13" :bar-width="1" />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="end">
<HuiSpace v-for="height in [40, 60, 80]" :key="height" direction="vertical">
<span>height={{ height }}</span>
<HuiBarcode value="NS2026" :height="height" />
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
文字
text 只替換展示文字,不改變編碼內容。自訂文字後取消商品碼分組和護線加長。text="" 與 display-value="false" 都隱藏文字區。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="start">
<HuiSpace direction="vertical"
><span>display-value=true</span><HuiBarcode value="NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>display-value=false</span><HuiBarcode value="NS2026" :display-value="false"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>自訂文字</span><HuiBarcode value="NS2026" text="訂單 NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>text 為空</span><HuiBarcode value="NS2026" text=""
/></HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace v-for="size in [12, 14, 18]" :key="size" direction="vertical">
<span>font-size={{ size }}</span>
<HuiBarcode value="NS2026" :font-size="size" />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace v-for="margin in [2, 8]" :key="margin" direction="vertical">
<span>text-margin={{ margin }}</span>
<HuiBarcode value="NS2026" :text-margin="margin" />
</HuiSpace>
<HuiSpace direction="vertical">
<span>font-family=serif</span>
<HuiBarcode value="NS2026" font-family="serif" />
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
顏色
條與文字共用 color。預設黑條白底,暗色主題也保持該配色。透明底適合放在已有淺色背景上。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace direction="vertical"><span>預設黑白</span><HuiBarcode value="NS2026" /></HuiSpace>
<HuiSpace direction="vertical"
><span>自訂顏色</span><HuiBarcode value="NS2026" color="#14532d" background-color="#f0fdf4"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>透明底</span><HuiBarcode value="NS2026" background-color="transparent"
/></HuiSpace>
</HuiSpace>
</template>
留白與靜區
左右留白取 padding 與格式規定靜區中的較大值。即使關閉靜區,商品碼仍為條碼外側的數字預留位置。掃碼時應保留足夠靜區。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="start">
<HuiSpace v-for="padding in [0, 10, 20]" :key="padding" direction="vertical">
<span>padding={{ padding }}</span>
<HuiBarcode value="NS2026" :padding="padding" bordered />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical"
><span>quiet-zone=true</span><HuiBarcode value="NS2026" :padding="0" bordered
/></HuiSpace>
<HuiSpace direction="vertical"
><span>quiet-zone=false</span
><HuiBarcode value="NS2026" :padding="0" :quiet-zone="false" bordered
/></HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
外框
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiSpace direction="vertical"
><span>bordered=false</span><HuiBarcode value="NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>bordered=true</span><HuiBarcode value="NS2026" bordered
/></HuiSpace>
</HuiSpace>
</template>
Code 39 檢查字元
checksum 為 Code 39 加入模 43 檢查字元,顯示文字仍為原始內容。其他格式不受此屬性影響。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiSpace direction="vertical"
><span>checksum=false</span><HuiBarcode value="NSUI-2026" format="CODE39"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>checksum=true</span><HuiBarcode value="NSUI-2026" format="CODE39" checksum
/></HuiSpace>
</HuiSpace>
</template>
狀態與刷新
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiMessage, HuiRadio, HuiRadioGroup, HuiSpace } from '@nscode/handy-ui'
import type { BarcodeStatus } from '@nscode/handy-ui'
const status = ref<BarcodeStatus>('expired')
const refresh = () => {
status.value = 'active'
HuiMessage.success('刷新請求已觸發')
}
</script>
<template>
<HuiSpace direction="vertical">
<HuiRadioGroup v-model="status" type="button">
<HuiRadio value="active">active</HuiRadio>
<HuiRadio value="loading">loading</HuiRadio>
<HuiRadio value="expired">expired</HuiRadio>
</HuiRadioGroup>
<HuiBarcode value="NS2026" :status="status" @refresh="refresh" />
<HuiBarcode
value="NS2026"
status="expired"
expired-text="訂單已過期"
refresh-text="重新生成"
@refresh="refresh"
/>
</HuiSpace>
</template>
自訂狀態插槽
<script setup lang="ts">
import { HuiBarcode, HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const refresh = () => HuiMessage.info('已請求重新取得條碼')
</script>
<template>
<HuiBarcode value="NS2026" status="expired" @refresh="refresh">
<template #status="{ status, refresh: requestRefresh }">
<HuiSpace direction="vertical" align="center">
<span>{{ status === 'expired' ? '訂單條碼已失效' : '正在取得條碼' }}</span>
<HuiButton icon="refresh" size="small" @click="requestRefresh">重新取得</HuiButton>
</HuiSpace>
</template>
</HuiBarcode>
</template>
即時驗證
空內容保留固定 100 模組寬度的預留空間,不顯示錯誤。非法內容顯示格式提示,開發環境警告中包含具體原因。invalid-text 可以覆蓋提示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiInput, HuiSelect, HuiSpace } from '@nscode/handy-ui'
import type { BarcodeFormat } from '@nscode/handy-ui'
const value = ref('690123456789')
const format = ref<BarcodeFormat>('EAN13')
const options = ['CODE128', 'EAN13', 'EAN8', 'UPC', 'CODE39', 'ITF14', 'ITF'].map((value) => ({
label: value,
value
}))
</script>
<template>
<HuiSpace direction="vertical">
<HuiSpace wrap>
<HuiSelect v-model="format" :options="options" aria-label="條碼格式" style="width: 160px" />
<HuiInput v-model="value" aria-label="條碼內容" style="width: 240px" />
</HuiSpace>
<HuiBarcode :value="value" :format="format" />
<HuiBarcode value="abc" format="EAN13" invalid-text="請填寫有效的商品號碼" />
</HuiSpace>
</template>
下載與 SVG 源碼
下載包含底色、留白和文字,不包含外框、狀態遮罩。位圖匯出等待頁面字體載入完成。匯出期間切換屬性,目前匯出仍使用呼叫時的內容和樣式。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiButton, HuiMessage, HuiSpace, HuiTextarea } from '@nscode/handy-ui'
import type { BarcodeExpose } from '@nscode/handy-ui'
const barcode = ref<BarcodeExpose | null>(null)
const source = ref('')
const download = async (type: 'image/png' | 'image/svg+xml') => {
try {
const url = await barcode.value?.toDataURL(type, { scale: 3 })
if (!url) return
const link = document.createElement('a')
link.href = url
link.download = type === 'image/png' ? 'barcode.png' : 'barcode.svg'
link.click()
HuiMessage.success('條碼已匯出')
} catch {
HuiMessage.error('匯出失敗,請檢查條碼內容')
}
}
const showSource = () => {
source.value = barcode.value?.toSvgString() ?? ''
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<div><HuiBarcode ref="barcode" value="690123456789" format="EAN13" /></div>
<HuiSpace wrap>
<HuiButton icon="download" @click="download('image/png')">下載 PNG</HuiButton>
<HuiButton icon="download" @click="download('image/svg+xml')">下載 SVG</HuiButton>
<HuiButton @click="showSource">顯示 SVG 源碼</HuiButton>
</HuiSpace>
<HuiTextarea v-model="source" readonly :rows="4" aria-label="SVG 源碼" />
</HuiSpace>
</template>
螢幕閱讀器文字
預設讀出完整號碼,商品碼包含補全的檢查碼。aria-label 優先於 alt;顯式空串同樣優先。
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiBarcode value="NS2026" alt="訂單號 NS2026" />
<HuiBarcode value="NS2026" alt="訂單號" aria-label="出庫單 NS2026" />
</HuiSpace>
</template>
Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| value | 編碼內容,空值時顯示預留空間 | string | '' |
| format | 編碼格式 | BarcodeFormat | 'CODE128' |
| barWidth | 最窄模組寬度,px,必須為正數 | number | 2 |
| height | 條高,px,不含留白和文字,必須為正數 | number | 60 |
| padding | 四周留白,px;左右仍至少保留靜區和外側數字 | number | 10 |
| quietZone | 是否保留格式要求的最小靜區 | boolean | true |
| color | 條與文字顏色 | string | '#000' |
| backgroundColor | 底色,含留白 | string | '#FFF' |
| displayValue | 是否顯示文字 | boolean | true |
| text | 自訂展示文字,傳入後取消商品文字分組 | string | undefined |
| fontSize | 字級,px,必須為正數;EAN、UPC 分組文字不超過 barWidth 的 10 倍,避免壓到護線 | number | 14 |
| fontFamily | 文字字體,頁面和匯出共用 | string | 'monospace' |
| textMargin | 文字與條的間距,px | number | 4 |
| checksum | Code 39 是否加模 43 檢查字元 | boolean | false |
| bordered | 是否顯示外框 | boolean | false |
| status | 目前狀態 | BarcodeStatus | 'active' |
| expiredText | 過期提示,支援空串 | string | 隨語言(中文:條碼已過期) |
| refreshText | 刷新按鈕文字,支援空串 | string | 隨語言(中文:點擊刷新) |
| invalidText | 非法內容提示,支援空串 | string | 隨語言並帶格式名 |
| alt | 替代文字,預設讀出完整號碼 | string | undefined |
| ariaLabel / aria-label | 螢幕閱讀器文字,優先於 alt | string | undefined |
Events
| 事件 | 說明 | 參數 |
|---|---|---|
| refresh | 點擊過期刷新按鈕或呼叫狀態插槽的 refresh | 無 |
Slots
| 插槽 | 說明 | 參數 |
|---|---|---|
| status | 自訂 loading / expired 遮罩 | { status: 'loading' | 'expired', refresh: () => void } |
方法
透過模板參照呼叫,類型為 BarcodeExpose。
| 方法 | 說明 | 簽名 |
|---|---|---|
| toSvgString | 匯出 SVG 源碼;空內容或非法內容回傳 '' | () => string |
| toDataURL | 匯出圖片;空內容或非法內容 reject,非法內容的錯誤名為 BarcodeValueError | (type?: BarcodeExportType, options?: BarcodeExportOptions) => Promise<string> |
類型
type BarcodeFormat = 'CODE128' | 'EAN13' | 'EAN8' | 'UPC' | 'CODE39' | 'ITF14' | 'ITF'
type BarcodeStatus = 'active' | 'loading' | 'expired'
type BarcodeExportType = 'image/png' | 'image/jpeg' | 'image/webp' | 'image/svg+xml'
interface BarcodeExportOptions {
/** 位圖倍數,預設 2;SVG 不受影響 */
scale?: number
/** JPEG / WebP 品質,0 到 1 */
quality?: number
}
interface BarcodeExpose {
toDataURL: (type?: BarcodeExportType, options?: BarcodeExportOptions) => Promise<string>
toSvgString: () => string
}
同時匯出 BarcodeProps、BarcodeEmits。