2

I have an HTML partial like:

<div v-on:customevent="test">
   <button @click="launchEvent">TestEvent</button>
</div>

In my component I have this:

methods:{
   launchEvent(){
      this.$emit('customevent');
   },
   test(){
      console.log("I'm in");   
   }
}

But even tt runs correctly function launchEvent it doesn't launch function test as the event was not caught because the event is correctly launched because if I try set in created():

this.$on('customevent',function(){
   alert("customevent launched");
});

The alert is correctly launched, so, Is it possible listen custom event directly on HTML?

3 Answers 3

1

Edited after OP pointed that the answer wasn't working

Those events are meant to be used in a parent-child component relationship, as explained in the documentation

So, in your example, you should have a parent component catching the event and the child component emitting it.

Your parent will looks like :

<parent @custom-event="customEventCatched">
  <child></child>
<parent>

And your child component's template should be like :

<button @click="$emit('custom-event')">Emit<button>
Sign up to request clarification or add additional context in comments.

3 Comments

thankyou! I tried but it doesn't work. I edit my question with your advise
jsfiddle.net/dangvanthanh/3m6s54qx following this jsfiddle I found problem
My bad, I forgot to say that those events are meant to notify a parent component, I'm updating the answer.
1

The syntax is incorrect in the first div, 'clicked' should be 'click', as in

<div v-on:click="test">Test Event</div>

or the short-hand syntax as used on the button,

<div @click="test">Test Event</div>

5 Comments

hello thank you, not, the name is exactly clicked, because i need a CUSTOM events, it could be named "customevent", but i would listen a custom event, not click event. I edit my answer for more clarity
$emit events is usually for communicating to the parent, but in case there's another reason to catch it in the same component, try defining the method with onTest:function() {...}
yes, following this jsFiddle I found the problem. jsfiddle.net/dangvanthanh/3m6s54qx
Could you post your fixed code in an answer please?
yes I answered my question my problem was a $emit, it was wrong because at first I had used a bus general, and the parent/child component. thankyou for your advise
0

My problem was that I not use correctly event:

now my code is:

<div>
   <MyComponent @customevent="eventCatched"></MyComponent>
</div>

And in MyComponent there is a function like:

test(){
   this.$emit("customevent");
}

In parent component I have a function eventCatched

eventCatched(){
  console.log("catched");
}

following this jsfiddle

https://jsfiddle.net/dangvanthanh/3m6s54qx/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.