Use external js lib (jsBarcode)


#1

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.


#2

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>

#3

Thank you so much!
It works like a charm!

ps: why we need this? public picture;


#4

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


#5

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