所以我试图在离子的空白页上实现 QR 扫描仪,但我在第一个"this"处收到一个错误,说意外的令牌,然后我在最后一个大括号处收到另一个错误,说声明或语句预期。
我对离子和打字稿很陌生,所以有点困难。这是直接从其本机 qrscanner 的离子文档中获取的。我也尝试了与其他人不同的代码,但通常会收到"this"的错误
下面是与该页面相关的整个代码。我还将插件添加到ngmodules中,以防万一。
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';
@IonicPage()
@Component({
selector: 'page-qrcode',
templateUrl: 'qrcode.html',
})
export class QrcodePage {
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private qrScanner: QRScanner){}
ionViewDidLoad() {
console.log('ionViewDidLoad QrcodePage');
}
this.qrScanner.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// camera permission was granted
// start scanning
let scanSub = this.qrScanner.scan().subscribe((text: string) => {
console.log('Scanned something', text);
this.qrScanner.hide(); // hide camera preview
scanSub.unsubscribe(); // stop scanning
});
// show camera preview
this.qrScanner.show();
// wait for user to scan something, then the observable callback will be called
} else if (status.denied) {
// camera permission was permanently denied
// you must use QRScanner.openSettings() method to guide the user to the settings page
// then they can grant the permission from there
} else {
// permission was denied, but not permanently. You can ask for permission again at a later time.
}
})
.catch((e: any) => console.log('Error is', e));
}
那是因为您没有将该代码部分包含在方法中......请参阅下面的thereShouldBeAMethodHere()
方法。如果要在创建QrcodePage
时执行该代码,可以从构造函数调用该代码。
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';
@IonicPage()
@Component({
selector: 'page-qrcode',
templateUrl: 'qrcode.html',
})
export class QrcodePage {
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private qrScanner: QRScanner) { }
ionViewDidLoad() {
console.log('ionViewDidLoad QrcodePage');
}
thereShouldBeAMethodHere() {
this.qrScanner.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// camera permission was granted
// start scanning
let scanSub = this.qrScanner.scan().subscribe((text: string) => {
console.log('Scanned something', text);
this.qrScanner.hide(); // hide camera preview
scanSub.unsubscribe(); // stop scanning
});
// show camera preview
this.qrScanner.show();
// wait for user to scan something, then the observable callback will be called
} else if (status.denied) {
// camera permission was permanently denied
// you must use QRScanner.openSettings() method to guide the user to the settings page
// then they can grant the permission from there
} else {
// permission was denied, but not permanently. You can ask for permission again at a later time.
}
})
.catch((e: any) => console.log('Error is', e));
}
}