简体   繁体   English

如何使用 NativeScript-vue 中的 BarcodeScanner 插件

[英]How to use BarcodeScanner plugin from NativeScript-vue

I am a beginner in NativeScript and need to scan a QR code.我是 NativeScript 的初学者,需要扫描二维码。 I tried to use the nativescript-barcodescanner plug-in with Vue.js and I couldn't, an error occurred while running all the demos.我尝试将 nativescript-barcodescanner 插件与 Vue.js 一起使用,但我不能,运行所有演示时出错。 I tried to find examples, but I found nothing to help me.我试图找到例子,但我发现没有任何帮助。

I wrote this code based on the demo-vue, but it also didn't work:我基于 demo-vue 编写了这段代码,但它也没有工作:

<template>
    <Page>
        <ActionBar title="BarcodeScanner demo"></ActionBar>

        <StackLayout>
            <Button text="back camera" @tap="doScanWithBackCamera"></Button>
            <BarcodeScanner
                row="1"
                height="300"
                formats="QR_CODE, EAN_13, UPC_A"
                beepOnScan="true"
                reportDuplicates="true"
                preferFrontCamera="false"
                @scanResult="onScanResult">
            </BarcodeScanner>

        </StackLayout>
    </Page>
</template>

<script>
    var BarcodeScanner = require("nativescript-barcodescanner").BarcodeScanner;

    export default {
        data() {
            return {

            }
        },
        methods: {
            onScanResult(evt) {
                console.log(`onScanResult: ${evt.text} (${evt.format})`);
            },
            doScanWithBackCamera() {
                this.scan();
            },
            scan() {
                var barcodescanner = new BarcodeScanner();
                barcodescanner.scan({
                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
                showTorchButton: true,        // iOS only, default false
                resultDisplayDuration: 500,   // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
                beepOnScan: true,             // Play or Suppress beep on scan (default true)
                openSettingsIfPermissionWasPreviouslyDenied: true, // On iOS you can send the user to the settings app if access was previously denied
                closeCallback: () => {
                    console.log("Scanner closed @ " + new Date().getTime());
                }
                }).then(
                    function (result) {
                    console.log("--- scanned: " + result.text);
                    // Note that this Promise is never invoked when a 'continuousScanCallback' function is provided
                    setTimeout(function () {
                        // if this alert doesn't show up please upgrade to {N} 2.4.0+
                        alert({
                        title: "Scan result",
                        message: "Format: " + result.format + ",\nValue: " + result.text,
                        okButtonText: "OK"
                        });
                    }, 500);
                    },
                    function (errorMessage) {
                    console.log("No scan. " + errorMessage);
                    }
                );
            }
        }
    }
</script>

<style scoped>
  ActionBar {
    background-color: #53ba82;
    color: #ffffff;
  }
</style>

The error to build demo-vue is:构建 demo-vue 的错误是:

[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\demo-vue\tsconfig.json
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR
[20-05-06 11:10:12.631] (CLI)       TS2688: Cannot find type definition file for 'node'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) C:/NativeScript/nativescript-barcodescanner-master/src/barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts(2,59)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/application'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) C:/NativeScript/nativescript-barcodescanner-master/src/barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts(3,28)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/application'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) C:/NativeScript/nativescript-barcodescanner-master/src/barcodescanner.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner.ts(4,24)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/utils/utils'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts(1,29)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/ui/content-view'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts(2,26)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/ui/core/properties'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts(3,34)
[20-05-06 11:10:12.631] (CLI)       TS2307: Cannot find module 'tns-core-modules/ui/core/view-base'.
[20-05-06 11:10:12.631] (CLI) ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts
[20-05-06 11:10:12.631] (CLI) [tsl] ERROR in C:\NativeScript\nativescript-barcodescanner-master\src\barcodescanner-common.ts(199,42)
[20-05-06 11:10:12.631] (CLI)       TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
[20-05-06 11:10:13.831] Build failed. Executing webpack failed with exit code 2.

I would like someone to explain to me how to use the plugin, else if someone has an example that can help me, else if they know another plugin that is easier to use.我希望有人向我解释如何使用该插件,否则如果有人有可以帮助我的示例,或者如果他们知道另一个更易于使用的插件。

Vue version of BarcodeScanner demo. Vue 版本的 BarcodeScanner 演示。

The main problem is: the demo is for old version of nativescript主要问题是:演示是针对旧版本的 nativescript

https://github.com/EddyVerbruggen/nativescript-barcodescanner/tree/master/demo-vue https://github.com/EddyVerbruggen/nativescript-barcodescanner/tree/master/demo-vue

The current application is not compatible with NativeScript CLI 6.0.当前应用与 NativeScript CLI 6.0 不兼容。 Use the tns migrate command to migrate the app dependencies to a form compatible with NativeScript 6.0.使用tns migrate命令将应用依赖迁移到与 NativeScript 6.0 兼容的形式。

Run tns migrate then tns doctor运行tns migrate然后tns doctor

tns doctor will say: Update available for component tns-core-modules. tns 医生会说:可用于组件 tns-core-modules 的更新。

Remove platforms and reinstall删除平台并重新安装

tns platform remove android
tns platform add android

your package.json file it should include你的 package.json 文件应该包括

"nativescript-barcodescanner": "^3.4.2",

Run tns debug android --no-hmr and check your console for errors.运行tns debug android --no-hmr并检查您的控制台是否有错误。


For a fresh start you should always prefer:对于一个新的开始,您应该始终喜欢:

1.Starting the nativescript-vue project using quick start 1.使用快速启动启动nativescript-vue项目

  1. Install Plugin using npm i nativescript-barcodescanner or yarn add nativescript-barcodescanner使用npm i nativescript-barcodescanneryarn add nativescript-barcodescanner安装插件

  2. Add the following line to your main.ts file.将以下行添加到您的main.ts文件中。 (after all import lines) (毕竟导入行)

Vue.registerElement('BarcodeScanner', () => require('nativescript-barcodescanner').BarcodeScannerView)
  1. Use App.vue file from vue demo project使用 vue 演示项目中的App.vue文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM