vue 条形码

一、JsBarcode

JsBarcode 文档

  1. 安装:
npm install jsbarcode --save
  1. 引入:在需要生成条形码的页面引入即可
import JsBarcode from 'jsbarcode'
  1. 需要显示条形码的页面里
<img id="barcode1">
  1. 调用构造函数生成条形码
let barCode1 = this.info.marIdCode;
let barheight = this.imgHeight;
JsBarcode("#barcode1",barCode1,{
    format:"CODE128", // 条形码的格式
    width:1,  // 线宽
    height:barheight,  // 条码高度
    lineColor:"#000",  // 线条颜色
    displayValue:false,  // 是否显示文字
    margin:2  // 设置条形码周围的空白区域
})
二、vue-barcode

vue-barcode 是JsBarcode的简单包装

  1. 版本一
    https://github.com/lindell/vue-barcode
    安装
npm install vue-barcode
  1. 版本二
    https://github.com/xkeshi/vue-barcode
    安装
npm install @xkeshi/vue-barcode 
  1. 两个版本区别

版本一:只能单独绑定属性
引入:在需要生成条形码的页面引入即可

 import  VueBarcode from 'vue-barcode'
 new Vue({
  components: {
      'barcode': VueBarcode
  }
})
<barcode  :value="barcodeValue"  :width="7"  :height="200"></barcode>

版本二:可直接options绑定所有属性
引入:在需要生成条形码的页面引入即可

 import VueBarcode from '@xkeshi/vue-barcode'
 new Vue({
  components: {
      'barcode': VueBarcode
  }
})
<barcode  :value="barcodeValue"  :options="{ width:100 , height:100}">
    条形码显示失败时的内容
</barcode>