没有BarcodeScanner的提供者

时间:2017-03-31 07:25:19

标签: angular cordova ionic-framework ionic-native

代码如下:


    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);
                });
        }
    }

错误图片:

enter image description here

参考:https://ionicframework.com/docs/native/barcode-scanner/



出现了新问题:
根据Suraj的建议,提示" plugin_not_installed"

错误图片:
enter image description here
enter image description here 开发环境: Visual Studio 2017

3 个答案:

答案 0 :(得分:9)

您需要在 app.module.ts

中将扫描仪设置为提供者
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@NgModule({
  ...

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

参考:Add Plugins to Your App's Module here

更新: 从你的package.json看起来你已经安装了较旧的ionic-native - 2.4.1。删除它并执行:

npm install --save @ionic-native/core

答案 1 :(得分:1)

安装ionic cordova plugin add phonegap-plugin-barcodescanner --save后,我在Android设备上遇到了同样的问题,错误消息为plugin_not_installed。只需在CAMERA_USAGE_DESCRIPTION的{​​{1}}标记内添加phonegap-plugin-barcodescanner变量并正常工作:)

config.xml

答案 2 :(得分:0)

我与ionic cli 3.9.2存在相同的问题,并使用npm i @ ionic-native / barcode-scanner @ beta --save解决了。

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

@NgModule({
  ...

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