在 nativescript-vue 应用程序中存储一个值
store a value in nativescript-vue app
我正在使用 nativescript-vue 制作一个简单的应用程序,在主页上我有一个启动扫描仪的按钮,因此我可以扫描产品。这是我的主页:
<template>
<Page class="page">
<StackLayout class="hello-world">
<Button @tap="scan" text="Scan a product"/>
</StackLayout>
</Page>
</template>
<script>
const BarcodeScanner = require("nativescript-
barcodescanner").BarcodeScanner;
import Display from "./Display";
export default {
data() {
return {
text: ""
};
},
methods: {
scan: function() {
var barcodescanner = new BarcodeScanner();
barcodescanner
.scan({
// formats: "QR_CODE,PDF_417", // Pass in of you want to restrict scanning to certain types
cancelLabel: "EXIT. Also, try the volume buttons!", // iOS only, default 'Close'
cancelLabelBackgroundColor: "#333333", // iOS only, default '#000000' (black)
message: "Use the volume buttons for extra light", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
showFlipCameraButton: true, // default false
preferFrontCamera: false, // default false
showTorchButton: true, // default false
beepOnScan: true, // Play or Suppress beep on scan (default true)
torchOn: false, // launch with the flashlight on (default false)
closeCallback: function() {
console.log("Scanner closed");
}, // invoked when the scanner was closed (success or abort)
resultDisplayDuration: 500, // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
// orientation: "landscape", // Android only, optionally lock the orientation to either "portrait" or "landscape"
openSettingsIfPermissionWasPreviouslyDenied: true // On iOS you can send the user to the settings app if access was previously denied
})
.then(
function(result) {
console.log("Scan format : " + result.format);
console.log("Scan text : " + result.text);
this.text = result.text;
},
function(error) {
console.log("No scan: " + error);
}
);
}
}
};
</script>
这里我遇到了一个问题,因为行 "this.text = result.text;" 不起作用,所以我不知道如何存储 result.text 值。
请在回调中使用箭头函数而不是普通函数。这是因为箭头函数没有将它自己的上下文绑定到 this
。相反,箭头函数在词法上绑定它们的上下文,因此 this
实际上指的是原始上下文。
解决方案:
barcodescanner
.scan({
...
})
.then(
(result) => {
console.log("Scan format : " + result.format);
console.log("Scan text : " + result.text);
this.text = result.text;
},
(error) => {
console.log("No scan: " + error);
}
);
}
我正在使用 nativescript-vue 制作一个简单的应用程序,在主页上我有一个启动扫描仪的按钮,因此我可以扫描产品。这是我的主页:
<template>
<Page class="page">
<StackLayout class="hello-world">
<Button @tap="scan" text="Scan a product"/>
</StackLayout>
</Page>
</template>
<script>
const BarcodeScanner = require("nativescript-
barcodescanner").BarcodeScanner;
import Display from "./Display";
export default {
data() {
return {
text: ""
};
},
methods: {
scan: function() {
var barcodescanner = new BarcodeScanner();
barcodescanner
.scan({
// formats: "QR_CODE,PDF_417", // Pass in of you want to restrict scanning to certain types
cancelLabel: "EXIT. Also, try the volume buttons!", // iOS only, default 'Close'
cancelLabelBackgroundColor: "#333333", // iOS only, default '#000000' (black)
message: "Use the volume buttons for extra light", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
showFlipCameraButton: true, // default false
preferFrontCamera: false, // default false
showTorchButton: true, // default false
beepOnScan: true, // Play or Suppress beep on scan (default true)
torchOn: false, // launch with the flashlight on (default false)
closeCallback: function() {
console.log("Scanner closed");
}, // invoked when the scanner was closed (success or abort)
resultDisplayDuration: 500, // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
// orientation: "landscape", // Android only, optionally lock the orientation to either "portrait" or "landscape"
openSettingsIfPermissionWasPreviouslyDenied: true // On iOS you can send the user to the settings app if access was previously denied
})
.then(
function(result) {
console.log("Scan format : " + result.format);
console.log("Scan text : " + result.text);
this.text = result.text;
},
function(error) {
console.log("No scan: " + error);
}
);
}
}
};
</script>
这里我遇到了一个问题,因为行 "this.text = result.text;" 不起作用,所以我不知道如何存储 result.text 值。
请在回调中使用箭头函数而不是普通函数。这是因为箭头函数没有将它自己的上下文绑定到 this
。相反,箭头函数在词法上绑定它们的上下文,因此 this
实际上指的是原始上下文。
解决方案:
barcodescanner
.scan({
...
})
.then(
(result) => {
console.log("Scan format : " + result.format);
console.log("Scan text : " + result.text);
this.text = result.text;
},
(error) => {
console.log("No scan: " + error);
}
);
}