代码如下:
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
。刚刚在CAMERA_USAGE_DESCRIPTION
的phonegap-plugin-barcodescanner
标签中添加了config.xml
变量并正在工作:)
<plugin name="phonegap-plugin-barcodescanner" spec="^7.0.1">
<variable name="CAMERA_USAGE_DESCRIPTION" value=" " />
</plugin>
我对离子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 { }