我正在开发一个应用程序,以读取使用BarcodeScanner的代码,但是当我在设备上执行该应用程序时,出现以下错误
**
**使用Ionic DevApp在我的iPhone 6上运行它时出错,错误运行时错误对象(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core _ [“ cordova”])不是函数。 (在'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core _ [“ cordova”])(此,“扫描”,{“ callbackOrder”:“反向”},自变量)','Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core _ [“ cordova”])是对象的实例
在离子服务中执行该错误时显示的错误,错误TypeError:Object(...)在MenuPage.webpackJsonp.101.MenuPage.scanQR(菜单)的BarcodeScanner.scan(index.js:31)处不是函数.ts:53)在Object.eval [作为handleEvent](MenuPage.html:17)在handleEvent(core.js:13589)在callWithDebugContext(core.js:15098)在Object.debugHandleEvent [作为handleEvent](core.js :14685),位于HTMLButtonElement的core.js:10629,位于dispatchEvent(core.js:10004)。 (platform-browser.js:2628)在t.invokeTask(polyfills.js:3)**
一开始我有一个错误,我的app-module.ts没有 识别我的BarcodeScanner,因为在提供者中调用它时 我,TS2322:类型“ BarcodeScannerOriginal”无法分配给该类型 “提供商”。类型“ BarcodeScannerOriginal”不可分配给该类型 “ FactoryProvider”。类型中缺少属性“提供” “ BarcodeScannerOriginal”
** Menu.html
<ion-header class="toolbar">
<ion-navbar>
<ion-title>Scan</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="Scan">
<div class="row">
<div class="col">
<h2>Scan your QR Code Here</h2>
</div>
<div class="col">
<h3>{{eventTitle}}</h3>
</div>
</div>
<button ion-button block color="secondary" class="Scan-button" (click)="scanQR()" [disabled]="loading">{{buttonText}}</button>
</ion-content>
Menu.ts
import { Component } from '@angular/core';
import {Platform} from "ionic-angular";
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { ToastController } from 'ionic-angular';
import { BarcodeScanner, BarcodeScannerOptions } from '@ionic-native/barcode-scanner/ngx';
/**
* Generated class for the MenuPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-menu',
templateUrl: 'menu.html',
})
export class MenuPage {
public scannedText: string;
public buttonText: string;
public loading: boolean;
private eventId: number;
public eventTitle: string;
num: string;
// @ts-ignore
constructor(private _nav: NavController,
private _navParams: NavParams,
private _barcodeScanner: BarcodeScanner) {
}
ionViewDidLoad() {
this.eventId = this._navParams.get('eventId');
this.eventTitle = this._navParams.get('eventTitle');
this.buttonText = "Scan";
this.loading = false;
}
public scanQR() {
this.buttonText = "Loading..";
this.loading = true;
this._barcodeScanner.scan().then((barcodeData) => {
if (barcodeData.cancelled) {
console.log("User cancelled the action!");
this.buttonText = "Scan";
this.loading = false;
return false;
}
console.log("Scanned successfully!");
console.log(barcodeData);
this.goToResult(barcodeData);
}, (err) => {
console.log(err);
});
}
private goToResult(barcodeData) {
this._nav.push(ScanResultPage, {
scannedText: barcodeData.text
});
}
}
app-module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import {MenuPage} from "../pages/menu/menu";
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import {HttpModule} from "@angular/http";
// @ts-ignore
@NgModule({
declarations: [
MyApp,
HomePage,
MenuPage
],
imports: [
BrowserModule, HttpClientModule,
IonicModule.forRoot(MyApp),
HttpModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
MenuPage
],
providers: [
StatusBar,
SplashScreen,
BarcodeScanner,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
答案 0 :(得分:1)
我的猜测是您使用的是Ionic 3,但您使用的是Ionic 4支持的本机插件版本。
解决方案
同时卸载cordova和ionic native插件
$ionic cordova plugin remove phonegap-plugin-barcodescanner
$npm uninstall @ionic-native/barcode-scanner
安装版本 4 。
$ ionic cordova plugin add phonegap-plugin-barcodescanner
$ npm install --save @ionic-native/barcode-scanner@4
并且不要在导入末尾附加 ngx 。
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
注意
如果您使用的是Ionic 3,请尝试遵循Ionic v3指南而不是最新指南
Ionic v3指南:https://ionicframework.com/docs/v3/native/barcode-scanner/
有关完整的解释,您可以在这里找到我的其他答案:https://stackoverflow.com/a/54474247/6617276