Use external js lib (jsBarcode)

Goodmorning,

I need to import this lib in my Ionic2 project (JsBarcode - a Barcode generation library written in JavaScript).

Can someone help me?
I can not import and use the functions.

Thx,
Best regards.

You’d install the package from npm

npm install jsbarcode --save

Then import it into your project


import { Component, ViewChild, ElementRef } from '@angular/core';
import JsBarcode from 'jsbarcode';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  public picture;
  @ViewChild('barcode') barcode: ElementRef;
  constructor(
  ) {
  }

  ionViewDidLoad(){
   JsBarcode(this.barcode.nativeElement, '12345');
  }
}
<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <svg #barcode id="barcode"></svg>
</ion-content>
2 Likes

Thank you so much!
It works like a charm!

ps: why we need this? public picture;

heh, must have been leftover from some testing I did earlier.

1 Like

Can you show how to convert the barcode to image?
trying to do it using canvas but stuck