cordova 没有BarcodeScanner的提供程序

d6kp6zgx  于 2022-11-15  发布在  其他
关注(0)|答案(3)|浏览(121)

代码如下:

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工作室2017

rbl8hiat

rbl8hiat1#

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

nhn9ugyo2#

我在使用离子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 { }
yh2wf1be

yh2wf1be3#

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

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

相关问题