Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
How add VueJS event listenner on a component in the mounted section

I important a componant and I want to attach an event listenner to the button element which has the class qkb-bubble-btn.

I understand the fact I should put this in the mounted section but I get the following error :

TypeError: Cannot read properties of undefined (reading 'getElementsByClassName')

Here is how I try to add the listenner :

<script>
import {VueBotUI} from 'vue-bot-ui'
import {subscribeUser} from "../helpers/subscriptionHandler";
//import {getAlerte} from "../helpers/alerteHandler";

export default {
//...
  mounted() {
    this.document.getElementsByClassName('qkb-bubble-btn ')[0].addEventListener('click', this.onBubbleClick);
  },
  beforeDestroy () {
    navigator.serviceWorker.removeEventListener('message', event => {
      this.printPush(event);
    });
    this.document.getElementsByClassName('qkb-bubble-btn ')[0].removeEventListener('click', this.onBubbleClick);
  },
  methods: {
    onBubbleClick(){
      console.log("click bubble")
    }
  },
}
</script>

How should I add the click event listenner to the qkb-bubble-btn element ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You don't need to add an event listener yourself,

you just use @ and the name of your event on the button :

<my-button @click="myClickMethod"/>

You only use addEventListener if the element is outside of vue app, but if it's inside the app, then better use vue way of doing it

And if the button is inside another component you will need to use $emit :

<my-button @click="$emit('click', $event)"/>
about 4 years ago · Juan Pablo Isaza Report

0

Even if @lk77 is right, I could fix my issue by removing the keyword this in front of this.document...

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!