Vue Integration Guide
This guide contains step-by-step instructions for integrating the STRICH Barcode Scanning SDK into a Vue 3 app. Examples will use the Composition API, the recommended approach to building modern Vue components.
Installing the SDK
Installing the SDK in a Vue project depends on your setup. Assuming you've created the app using the standard npm create vue@latest path (see Vue Quick Start). If you're using npm, add the SDK to your project:
This will install the latest version of the SDK. If barcode scanning functionality is a critical part of your app, we recommend pinning a version of the library.
SDK Initialization
To start using the SDK in your Vue app, you'll need a license key. Check out the Getting Started guide if you haven't already done that for instructions on how to obtain a license key.
The SDK is initialized using StrichSDK.initialize(). This has to be called before using any of STRICH's barcode scanning integrations (PopupScanner or BarcodeReader).
Using the PopupScanner
STRICH ships with an out-of-the-box scanning experience: the Popup Scanner. It can be used to quickly start scanning barcodes with minimal integration effort.
The popup scanner is invoked by calling PopupScanner.scan(). In the example below, the scanner is integrated into a Vue SFC (Single File Component) and configured to detect QR Codes and Code 128 barcodes. If a code was scanned, the decoded value will be displayed. The user may also cancel the dialog, which is indicated by a return value of undefined.
The snippet initializes the SDK every time scanning is triggered, which isn't optimal, as there might be a short delay on the first call. You might decide to initialize the SDK when the Vue application is created, or in a route guard, see the Advanced Topics section below.
Using the Popup Scanner frees you from managing the BarcodeReader lifecycle, and is recommended for simple scanning scenarios.
Using BarcodeReader
If you need tighter integration with the rest of your app, you can choose to integrate BarcodeReader directly, which is a lower-level way of accessing the SDK functionality.
One way to achieve that is by creating a Vue component that wraps a BarcodeReader and provides it in a reusable way to the rest of Vue app.
Encapsulating BarcodeReader in a Vue Component
The snippet below shows a Vue SFC that wraps a BarcodeReader. The snippet is also available in a more complete form in our Vue 3 sample repository on GitHub.
Vue Scanner Component
Key Implementation Aspects
The key aspects of the implementation are:
The host element which will contain the BarcodeReader is provided through the component's HTML template and referenced through a
ref.The component declares an emitted event
detectedto notify its consumer of a detected barcode. In the snippet, we use the newer Vue 3.3+ syntax for [typed emits](https://vuejs.org/api/sfc-script-setup.html#type-only-props-emit-declarations.The component exposes start and stop functions to its consumer, for starting/stopping barcode recognition.
The component manages the BarcodeReader lifecycle by coupling initialization and destruction to the component's mount/unmount lifecycle hooks.
Props could be used to inject context-specific configuration for the BarcodeReader, e.g. for detecting only QR Codes in one screen, Code 128 barcodes in another.
Advanced Topics
Ensuring SDK Initialization via a Route Guard
In the sample code, we use Vue's client-side router and router guards, to ensure the SDK is initialized before entering a screen that needs it.
Router Initialization
Navigation Guard Implementation
Sample Code
Full sample code for Vue 3 is available in our Vue 3 sample repository on GitHub.