Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a cordova plugin to Ionic 2

I used this plugin in ionic v1, workd fine. But Im not sure how to add it to Ionic 2

Cant build the project because it cant find cordova

import { Component } from '@angular/core';
import { Platform, NavController, NavParams } from 'ionic-angular';


@Component({
    selector: 'page-scan-vehicle',
    templateUrl: 'scan-vehicle.html'
})
export class ScanVehiclePage {

    constructor(public platform: Platform, public navCtrl: NavController, public navParams: NavParams) {}

    public scan () {
        // open scanner
        // save results
        this.platform.ready().then(() => {
            cordova.plugins.pdf417Scanner.scan()
        });
    }

}

Enev in this example they are doing the same. But how would it work if it does not find cordova, I mean cordova is only added once built isnt it??

like image 372
Harry Avatar asked Jan 25 '17 12:01

Harry


1 Answers

The main difference of your code and the example that you gave us is this one:

Example

app/pages/home/home.js

Your code (I would guess that you used ionic-cli to generate the page)

scan-vehicle-page.ts

Try with this:

(<any>cordova).plugins.pdf4157Scanner.scan();

That is a workaround to avoid issues with typescript, because cordova doesn't exists on that scope, but you know that it exists on runtime.

like image 92
dlcardozo Avatar answered Sep 25 '22 01:09

dlcardozo