简体   繁体   中英

Vue 3 - Prevent event propagation on custom event handlers

If i have a custom event handler like @my-event and i emit it with

this.$emit('my-event')

it calls all the events of @my-event from all parent components. But i want to prevent this. On normal event handlers like @click , we can simply add .stop behind it to prevent this behaviour. But .stop does not work on custom event listeners.

Fiddle without stop: https://jsfiddle.net/xn9sq4cp/

Fiddle with stop: https://jsfiddle.net/xy18mspz/

I know i can add

inheritAttrs: false

to prevent event propagation globaly, but i dont want this.

So, how can i prevent event propagation for custom event handlers.

Thanks.

You can stop the component from emitting the event to stop it from being bubbled up by adding this:

// ...
emits: {
  'my-event': false
}
// ...

JS Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM