Troubleshooting: Vue.js component events not being detected in TypeScript

I'm encountering an issue with receiving events from a barcode reader, which I heavily referenced from a GitHub repository.

The problem lies in the fact that the events emitted by the BarcodeScanner component are not being captured by the enclosing component, a modified version of HelloWorld.vue. The Vue DevTools in the browser console indicate that the events are being properly emitted. However, the HelloWorld component does not respond to these events.

HelloWorld.vue:

<template>
  <div class="hello">
    <barcode-scanner @loaded="onLoaded" @decoded="onDecoded"/>
  </div>
</template>

<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import BarcodeScanner from "@/components/BarcodeScanner.vue";

@Component({
  components: {BarcodeScanner}
})
export default class HelloWorld extends Vue {

  onLoaded() {
    console.log("Barcode scanner component has emitLoaded");
  }

  onDecoded(code: string) {
    console.log("Scanned code", code);
  }
}
</script>

<style scoped>
</style>

BarcodeScanner.vue:

<template>
  <div class="scanner-container">
    <div v-show="isLoaded">
      <video ref="scanner"></video>
      <div class="overlay-element"></div>
    </div>
  </div>
</template>

<script lang="ts">
import {Component, Emit, Ref, Vue} from "vue-property-decorator";
import {BrowserMultiFormatReader, Exception, Result} from "@zxing/library";

@Component
export default class BarcodeScanner extends Vue {

  @Ref("scanner") private scanner!: HTMLVideoElement;
  private isLoaded = false;
  private codeReader = new BrowserMultiFormatReader();
  readonly streamAPISupported = ("enumerateDevices" in navigator?.mediaDevices);

  @Emit("loaded") emitLoaded(): void {
    this.isLoaded = true;
  }

  @Emit("decoded") emitDecoded(code: string) {
    console.log("Decoded", code);
  }

  mounted() {
    if (!this.streamAPISupported) {
      throw new Exception("Media stream API not supported");
    }
    this.start();
    this.scanner.oncanplay = () => {
      this.emitLoaded();
      console.log("Scanner has loaded (BarcodeScanner component)");
    }
  }

  start(): void {
    this.codeReader.decodeFromVideoDevice(
        null,
        this.scanner,
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        (result: Result, err: Exception | undefined) => {
          if (result) {
            console.log(result);
            this.emitDecoded(result.getText());
          }
        }
    )
  }
}
</script>

<style scoped>
video {
  max-width: 100%;
  max-height: 100%;
}

.scanner-container {
  position: relative;
}

.overlay-element {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(30, 30, 30, 0.5);
  -webkit-clip-path: polygon(
      0% 0%,
      0% 100%,
      20% 100%,
      20% 20%,
      80% 20%,
      80% 80%,
      20% 80%,
      20% 100%,
      100% 100%,
      100% 0%
  );
  clip-path: polygon(
      0% 0%,
      0% 100%,
      20% 100%,
      20% 20%,
      80% 20%,
      80% 80%,
      20% 80%,
      20% 100%,
      100% 100%,
      100% 0%
  );
}
</style>

Is there something obvious that I may be overlooking?

Answer №1

Solving the issue by recreating my Vue.js project and transferring the files mentioned above unexpectedly resolved the problem.

With a background in Java and more than twenty-five years of professional experience, I continue to be bewildered by certain aspects of modern front-end frameworks.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am attempting to create a footer with a set size, but encountering an issue

I'm in the process of developing a responsive website using Vue.js. One aspect I am working on is the footer container, which looks fine on the full screen but shrinks when the screen resolution is reduced below 1100 pixels. As shown in the images li ...

Discover the data type of the subfield within an interface or type in Typescript

Check out the interface and type declarations provided below: interface Foo { bar: { a: number b: string } } type Foo = { bar: { a: number b: string } } Is it possible to obtain the type definitions for "baz"? This will allow us ...

Failure to trigger an event in Vue.js

I've implemented an overlay feature for my modal that should toggle the modal when clicked. Here's the code snippet I have in my mounted() method: const overlay = document.querySelector(".modal-overlay"); overlay.addEventListener("click", this ...

vue v-if="canEdit" @click.prevent

I have a Vue component called 'test' with specific template and methods. The goal is to make the div clickable only if helper.isEditable is true, otherwise it should be notClickable or touchable. However, I can only retrieve the value of helper. ...

Ways to alter and remove ArrayList arrangement

I am new, so please excuse all the questions. Could you kindly explain to me? <ul id="example-1"> <li v-for="item in items"> {{ item.message }} <v-btn>up</v-btn> <v-btn>down</v-btn> ...

Utilizing Vue 3/Nuxt 3 Scoped Slots to Automatically Deduce Generic Data Types from Props

I am looking to incorporate a carousel component into Nuxt v3. The component will be passed an array of items, focusing solely on the logic without handling styling or structuring. Currently, this is what my component looks like: components/tdx/carousel. ...

What are some ways to incorporate advanced/nested type variables when using arrow functions?

Is there a way to use "advanced/nested" type variables, similar to how T is utilized in this function declaration, when working with arrow functions? function wrapInObject<T>(key: string) { return (x: T) => ({ [key]: x }); } I attempted to ach ...

Restricting HTTP requests to once every 200 milliseconds in Angular 4 with filtering in place

In my current project, I am working on a page that utilizes an ngFor to display an array of objects. One of the features I want to implement is the ability for users to filter these objects by entering specific keywords in an input field. Since the data f ...

Is it possible to define a constant enum within a TypeScript class?

I am looking for a way to statically set an enum on my TypeScript class and be able to reference it both internally and externally by exporting the class. As I am new to TypeScript, I am unsure of the correct syntax for this. Below is some pseudo-code (whi ...

How to access saved values in WebdriverIo using Typescript

I am currently using WebdriverIO along with Typescript for automating an Android app. Scenario: Go to the Training Page Get the Session name (This value changes dynamically) I want to store the retrieved session name in a variable and then later assert ...

Tips for accessing the return value from a method outside of its containing function

Having recently started using Angular, I'm encountering an issue with retrieving a return value from a function that I have implemented within another one. private validateKeyterm(): boolean { const val = this.form.value.term; if ...

Retrieving the chosen option using a change event listener

Is there a way to retrieve the selected option using a change listener in TypeScript? I have come across JavaScript examples where the value is retrieved through event., but I am unable to locate any field that contains the selected option. <!DOCTYPE ...

The "maxfilesexceeded" event in dropzone.js does not seem to be triggered when adding files programmatically

In my Vue.js project, I am using dropzone with the maxFiles: 1 option set. To display an existing file from the server in dropzone, I have added the following code: let mockFile = { name: 'Filename', size: file.size }; myDropzone.emit('added ...

Buefy fails to respond to Sass styling efforts

Hey there, I'm new to Vue and I've set up a simple Vue (2) app with node-sass and sass-loader following the instructions here. I have Buefy imported in main.js and below is an excerpt from my App.vue file: <style lang="scss"> // ...

How to pass data/props to a dynamic page in NextJS?

Currently, I am facing a challenge in my NextJS project where I am struggling to pass data into dynamically generated pages. In this application, I fetch data from an Amazon S3 bucket and then map it. The fetching process works flawlessly, generating a se ...

Unable to retrieve func from PropTypes

Struggling to understand why this specific import, among others, is not functioning properly: import * as React from 'react'; import TextField from '@material-ui/core/TextField'; import * as PropTypes from 'prop-types'; impor ...

What is the best way to manage variables that are not present in a Vue.js template?

When working with vue.js templates, I often come across instances like this: {{ jobs[0].stages[0].node.name }} If a job has no stages, the entire template fails to load and vue.js admin throws this warning: Error in render: "TypeError: Cannot read prope ...

What is the TypeScript's alternative to ReasonML's option type?

When using ReasonML, the option type is a variant that can be either Some('a) or None. If I were to represent the same concept in TypeScript, how would I go about it? ...

What is the best way to simulate global variables that are declared in a separate file?

dataConfiguration.js var userData = { URIs: { APIURI: "C" }, EncryptedToken: "D" }; configSetup.js config.set({ basePath: '', files:['dataConfiguration.js' ], ..... UserComponentDetails: .....some i ...

Bar chart in Chart.js becomes crowded and illegible on smaller screens due to overlapping bars

Hello there! I've encountered an issue where the bar chart overlaps when the screen width is too low. It seems to be related to the maintainAspectRatio property, which I set to false because I wanted the charts to shrink only in width, not in both axe ...