Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no provider for BarcodeScanner

The code is as follows:


    import { Component } from '@angular/core';
    import { NavController, Platform } from 'ionic-angular';
    import { TranslateService } from '@ngx-translate/core';
    import { BarcodeScanner } from '@ionic-native/barcode-scanner';

    @Component({
        selector: 'page-home',
        templateUrl: 'home.html'
    })
    export class HomePage {
        constructor(public navCtrl: NavController, private translate: TranslateService, private barcodeScanner: BarcodeScanner) {

        }
        //Switch language 
       changelanguage() {
            let browserLang = this.translate.currentLang;
            this.translate.use(browserLang.match(/en/) ? 'zh' : 'en');
        }
        //Scan test
        ScanCode() {
            this.barcodeScanner.scan()
                .then((result) => {
                    console.log(result);
                })
                .catch((error) => {
                    alert(error);
                });
        }
    }

Error Picture:

enter image description here

Reference:https://ionicframework.com/docs/native/barcode-scanner/



new questions arose:
According to Suraj recommendations, the prompt "plugin_not_installed"

Error Picture:
enter image description here
enter image description here development environment: Visual Studio 2017

like image 979
路见不平一声吼 Avatar asked Mar 31 '17 07:03

路见不平一声吼


1 Answers

You need to set the scanner as provider in app.module.ts

import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@NgModule({
  ...

  providers: [
    ...
    BarcodeScanner
    ...
  ]
  ...
})
export class AppModule { }

Reference: Add Plugins to Your App's Module here

UPDATE: From your package.json it looks like you have installed older ionic-native - 2.4.1.Remove it and do:

npm install --save @ionic-native/core
like image 121
Suraj Rao Avatar answered Oct 23 '22 18:10

Suraj Rao