What sets v-on:event apart from this.$on(event, handler) in VueJs?

I've been delving into Vue.js event handling and I believe developers can utilize this.$on('event', handler) in their JavaScript files to handle the 'event'.

An interesting example demonstrates this concept.

<div id="mainapp" v-on:event="processEventFromView">
    <button type="button" v-on:click="emitEvent">
       Emit Event
    </button>
</div>

JavaScript File

var app = new Vue({
  el:"#mainapp",
  data:{
    show:false
  },
  created:function(){
     this.$on('event', this.processEvent);
  },
  methods:{
      emitEvent:function(){
          this.$emit('event', {data:'mydata'});
      },
      processEvent(data){
         console.log('JavaScript', data);  //this is triggered when the button is clicked.
      },
      processEventFromView(data){
         console.log('view', data);  //this is not triggered at all.
      }      

   }
})

In the example, only the handler processEvent which is attached via this.$on() gets called upon clicking the button. What sets v-on apart from this.$on?
Why doesn't

 v-on:event="processEventFromView"
get executed at all?
Instead of using v-on:click="emitEvent", could an event handler be linked to the button's click event through a reference (ref)?
I'd appreciate any help in identifying where I might have gone wrong.

Answer №1

Linus Berg tackled a similar issue in Vue on Stack Overflow, check out his response here. Even though it pertains to an older version of Vue (dating back to 2016), the solution still holds true.

To sum it up, the answer to your question

Why is v-on:event="processEventFromView" not called whenever?

can be found in this quote:

You cannot use v-on:custom-event-name in the template (it should only be used on components).

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

Establishing session management for tasks in Node.js

I'm currently developing a web application using Node JS and encountering an issue with the session store in req. For my sessions to work, I have set up my app.js like this: // Enable sessions app.use(session({ secret: '***********', ...

Class for making elements draggable using jQuery UI

Is it possible to use jQueryui's draggable/droppable combo and add a class to the dragged item when dropped into a specific container, rather than adding a class to the container itself? I've tried adding a class to the container, but that is not ...

The mute feature in Discord.js along with setting up a muterole seems to be malfunctioning and encountering errors

Currently, I am working on implementing a mute command feature. The main goal is to have the bot automatically create a role called Muted if it doesn't already exist, and then overwrite the permissions for every channel to prevent users with that role ...

Dynamic sliding box jumps instead of simply fading in/out

My app features both a navigation bar and a sub-navigation bar. Within the sub-navigation bar, users can click on a button that triggers the appearance of another sub-bar while hiding the original one. The new sub-bar should smoothly slide out from behind ...

Hide a single column in a Vue b-table

As a newcomer to Vue, I am using Vue and Bootstrap-vue to paginate my data from nameList in this project. Is there a way I can hide the column nameList.id and only display the first_name and last_name? Check out the code on JsFiddle = https://jsfiddle.net ...

Discovering the position of elements in relation to their (grand^N)parent

Among my possessions are elements A and B. B has the ability to exist as a direct child of A, or there may be anywhere from 0 to N elements separating them. It is imperative that I determine how B is situated in relation to A, specifically noting the dist ...

Troubleshooting: Vue.js custom select element not responding to blur event

Unique Scenario A custom autocomplete select component has been created that not only autocompletes but also allows for adding a new value if the result is not found. This functionality was discovered to be missing in the vue-select module. Explore the C ...

What is the best practice for storing a SQLITE database file in electron when in production mode?

Currently, I am developing a portable Node.js server using Electron and utilizing an SQLITE database for data storage. During development, the database file test.db is placed in the same directory as my Main.js file, which works perfectly. However, when I ...

Testing Angular 16 Component with Jasmine Spy and callFake Strategy

I've encountered an issue while trying to test my component unit. The problem arises when I call the product-list.component.ts from my product.service.ts. While my product.service.spec.ts is successful, the product-list.component.spec.ts fails as the ...

Generate a complete screenshot of a website using Chrome 59 through code

Is it possible to programmatically capture a full-page screenshot of a website using the latest Chrome 59 and chromedriver with Selenium Webdriver, even if the website is larger than the screen size? ...

Can anyone tell me what I might be doing incorrectly when comparing these two timestamps?

I am facing an issue while comparing two timestamps in Angular. Here is the code snippet: public isAuthenticated(): boolean { const token = localStorage.getItem('Fakelife'); const lifetime = new Date().getTime(); const result = life ...

What is the best way to combine vuetify.css and Bootstrap on a single webpage?

Is it possible to simultaneously incorporate the vuetify.min.css and bootstrap.min.css folders in a project? Bootstrap is currently implemented on the layout page while vuetify.min.css is needed for another specific page. How can both of these CSS framew ...

Vue.js and webpack are having trouble loading the images located in the "assets" folder into the Vue components

How can I import images from an assets folder into Vue components? Here is my "Card.vue" component: <template> <div class="container"> <div class="card" style="width: 18rem;"> <!-- Use this <img> tag to load the ima ...

Extract specific data from JSON objects

I have a JSON string that I need to parse on the client side in order to extract data for three different column names (MAT_ETHNICITY, PAT_ETHNICITY, SEX). My goal is to populate drop down lists with the values. Should I handle this by making three separ ...

Adding a class to a TD element only when there is text in that TD as well as in other TD elements

I am facing a challenge where I need to add a new class to a td element, specifically the first one in the code snippet below. However, this should only happen if that td contains a certain number (e.g., "2") and if the next td contains some specific text ...

Is there a way to access the value of a variable that is defined in FileReader.onloadend from within the callback function of XMLHttpRequest.onreadystatechange

I'm trying to access the value of the variable ext (defined within the function reader.onloadend) in the function xhr.onreadystatechange. Currently, it is returning undefined. Does anyone have any suggestions on how to resolve this issue? function pr ...

Is there a way to determine if the items in an array are duplicated?

Hello all, as a beginner in coding, I'm looking for guidance on determining whether an array contains repeated objects regardless of their order, without knowing the specific objects in advance. Let's say: I initialize an empty array: var rando ...

What is the best method to determine the currency associated with the code in dinero.js?

Is there an easy way to locate the dinero currency in the dinero.js/currencies package using its code? For example, a function called getCurrency that accepts a string as input and outputs the corresponding dinero currency. ...

Indicator malfunctioning on Carousel feature

I created a carousel containing six photos, but I am encountering an issue with the carousel indicators below. The first three indicators work correctly – when clicked, they take me to the corresponding slide (e.g., clicking the 1st indicator takes me ...

Tools for diagnosing WebGL Three.js issues

Yesterday, I posed a question and now I want to take a different approach while still keeping the previous discussion going. (The initial question was about variable frame rates in Three.js.) Instead of directly addressing that question, I'm curious ...