index.js 954 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { defineComponent, h } from 'vue'
  2. import JsBarcode from 'jsbarcode'
  3. export default defineComponent({
  4. name: 'VueBarcode',
  5. props: {
  6. /**
  7. * The value of the bar code.
  8. */
  9. value: {
  10. type: String,
  11. default: undefined
  12. },
  13. /**
  14. * The options for the bar code generator.
  15. * {@link https://github.com/lindell/JsBarcode#options}
  16. */
  17. options: {
  18. type: Object,
  19. default: undefined
  20. },
  21. /**
  22. * The tag name of the component's root element.
  23. */
  24. tag: {
  25. type: String,
  26. default: 'canvas'
  27. }
  28. },
  29. watch: {
  30. $props: {
  31. deep: true,
  32. immediate: true,
  33. /**
  34. * Update the bar code when props changed.
  35. */
  36. handler() {
  37. if (this.$el) {
  38. this.generate()
  39. }
  40. }
  41. }
  42. },
  43. mounted() {
  44. this.generate()
  45. },
  46. methods: {
  47. /**
  48. * Generate bar code.
  49. */
  50. generate() {
  51. JsBarcode(this.$el, String(this.value), this.options)
  52. }
  53. },
  54. render() {
  55. return h(this.tag, this.$slots.default)
  56. }
  57. })