没有 BarcodeScanner 的提供者

no provider for BarcodeScanner

代码如下:


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

错误图片:

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



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

错误图片:

开发环境: Visual Studio 2017

您需要在 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

我在安装 ionic cordova plugin add phonegap-plugin-barcodescanner --save 后在 android 设备上遇到了同样的问题,错误消息是 plugin_not_installed。刚刚在 config.xml 处的 phonegap-plugin-barcodescanner 标签内添加了 CAMERA_USAGE_DESCRIPTION 变量并开始工作 :)

<plugin name="phonegap-plugin-barcodescanner" spec="^7.0.1">
    <variable name="CAMERA_USAGE_DESCRIPTION" value=" " />
</plugin>

我对 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 { }