Why is the parent element not receiving the event in Vue.JS? Is it necessary for the parent to be a custom component in order to receive the

I'm new to Vue.js. I used to think that Events worked by bubbling up through the DOM tree until caught by a parent element. However, something seems to be off. The code below isn't functioning as expected - there are no errors or warnings, and in the Vue developer tools, I can see the event being emitted. What am I overlooking?

index.html:

<!doctype html>
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e79192829fa7d4c9d6c9d5">[email protected]</a>/dist/vuex.js"></script>

</head>
<body>

    <div id="app" @button-clicky="gotevent">
        <div @button-clicky="gotevent">
            <h1>{{ info }} {{ counter }}</h1>
            <button-component />
        </div>
    </div>

    <script src="./main.js"></script>
</body>
</html>

main.js:

Vue.component('button-component', {
    data: function() {
        return {
            count: 1
        }
    },

    methods:
    {
        clicky() {
            this.$data.count++;
            this.$emit('button-clicky');
        }
    },

    template: `<button @click="clicky">Go {{ count }}</button>`
});

const vue = new Vue({
    el: '#app',
    data: {
        info: "this is title.",
        counter: 0
    },

    methods: {
        gotevent() { 
            this.$data.counter++;
            alert('The event was heard!'); }
    }
});

Is it not possible to listen for an event on any parent level above the component emitting it?

I've added the event listener both on the direct parent <div> and the 'app' container, but nothing happens - no errors or warnings.

Answer №1

Vue events don't seem to bubble like DOM events do, so it's important to capture them on the emitting component. For example, you can use

<button-component  @button-clicky="gotevent" />

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

Error occurred within node while attempting to create a directory using mkdirsync

I wrote some code. try{ var Storage = multer.diskStorage({ destination: function (req, file, callback) { fs.mkdirSync('/home/data/' + file.originalname, { recursive: true }, (error) => { ...

The value of $parent.$index will consistently be 0 in all cases

I am currently dealing with a nested ng-repeat situation where I am attempting to determine the parent's index. Due to the fact that it is being arranged post-process, the standard ng-repeat="(step_index, step) in flow" method is not working for m ...

tsconfig.json respects the baseUrl for absolute imports inconsistently

While using create-react-app, I have noticed that absolute imports work in some files but not in others. Directory Layout . +-- tsconfig.js +-- package.json +-- src | +-- components | | +-- ui | | | +-- Button | | | | +-- Button.tsx | ...

Guide to activating the 'Next' button on WP Forms using JavaScript code when certain conditions are fulfilled

Is there a way to adjust the JavaScript code provided so that the visibility of the "Next" button is dependent on whether at least one item in the 'wpforms-icon-choices-item' class has the '.wpform-selected' class on the current page or ...

Sequelize cannot locate the specified column in the database

click here for image description I encountered an issue where a column was not recognized after migrating with sequelize-cli. Even though all the columns are defined in the migration file, this error keeps appearing. Additionally, when trying to create a ...

Two jQuery scripts failing to cooperate

My website is built using jQuery 2.0.3 and bootstrap. I have implemented two different functions in jQuery, one for creating a dropdown menu and another for changing images on page load. $(document).ready(function() { //To display a random image eve ...

Refreshing the "OnMounted" Vue3 component

Recently, I encountered a situation where I have a Vue3 Dynamic Component enclosed within a <keep-alive> tag. This component facilitates navigation within a PrimeVue <Dialog> and is populated by an object: const component = [ { id: 0, ...

Is it feasible for a form button to perform multiple actions simultaneously?

I am interested in exploring the possibility of having a submit form button perform multiple actions. Currently, I have a custom form that is sent to a Google Spreadsheet using AJAX and I am also utilizing the Blueimp Jquery File Upload plugin. My goal is ...

Is Javascript Functioning Properly?

Similar Question: How do I determine if JavaScript is turned off? Can we identify whether javascript has been disabled and then redirect to a different page? I am currently working on a project using JSPs, where I have integrated various advanced java ...

A guide to retrieving the timezone based on a specific address using the Google API

I need to utilize the Google API time zones, which requires geocoding the address to obtain the latitude and longitude for the time zone. How can I achieve this using a value from a textarea? Here are the 2 steps: Convert the textarea value into a geoc ...

I am a beginner in the world of MEAN stack development. Recently, I attempted to save the data from my form submissions to a MongoDB database, but unfortunately, I have encountered

const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); const Schema = new mongoose.Schema({ username: St ...

Error in Typescript: Function declarations are not allowed in blocks while in strict mode

When attempting to run a script, I encountered the following error message: Function declarations are not permitted in blocks in strict mode during targeting of the 'ES3' or 'ES5' version. The modules are automatically in strict mode. ...

What could be causing the "10 $digest error" to appear in my code?

My goal was to create a basic app that could detect the size of the browser and display it. But, I encountered an error message saying "Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!" app.controller('AppCtrl',function($win ...

Having difficulty transferring data from a JSON file on a different domain to a variable using Ajax

As a beginner in Ajax, I am currently exploring the use of Ajax Cross domain functionality to fetch data. The Ajax function is triggered from test.php, which then calls stats.php to request the desired data. The content of Stats.php: <?php $data = ...

The values are not being displayed in the local storage checkbox

I attempted to transfer checkbox values to another HTML page using local storage, however they were not appearing on the other page Here is my page1 HTML code snippet: <input class="checkbox" type="checkbox" id="option1" name="car1" value="BMW"> ...

Unable to retrieve data from SpringBoot controller using $http.get request

I have developed an application that will execute queries on my store's database based on user input on the webpage. The backend method is functioning correctly and returns the response, but I am having trouble displaying the data in a dynamic table o ...

Iterating recursively through a tree structure to update properties using Mongoose

I have a unique structure resembling a tree that I've set up to store comments. Each "comment" acts as a node with a "parent" property linking it to another "comment" node. Additionally, I've included a "replyCount" field on each node to keep tra ...

The state of the button remains unchanged in jQuery/Rails 3

After immersing myself in the Hartl Rails 3 tutorial for a few days (about 4 to be exact), I encountered an issue while attempting to convert the site from prototype to jQuery in chapter 12. The problem lies in getting the "follow"/"unfollow" button to upd ...

Issue with Animation Pause Functionality

I'm currently working on a music loader feature and I'm trying to create a toggle switch to pause and play the music. So far, I've been struggling with this functionality but managed to get it partially working on a simpler project, which yo ...

Exploring the functionality of two-way data binding in Angular - a beginner's guide

Transitioning from a different framework and switching from JavaScript to Angular & TypeScript has left me feeling confused about how to efficiently share data/values between components. ...