简体   繁体   English

Vue 3 - 防止自定义事件处理程序上的事件传播

[英]Vue 3 - Prevent event propagation on custom event handlers

If i have a custom event handler like @my-event and i emit it with如果我有一个像@my-event这样的自定义事件处理程序,我用

this.$emit('my-event')

it calls all the events of @my-event from all parent components.它从所有父组件调用@my-event的所有事件。 But i want to prevent this.但我想阻止这种情况。 On normal event handlers like @click , we can simply add .stop behind it to prevent this behaviour.在像@click这样的普通事件处理程序上,我们可以简单地在它后面添加.stop来防止这种行为。 But .stop does not work on custom event listeners.但是.stop不适用于自定义事件侦听器。

Fiddle without stop: https://jsfiddle.net/xn9sq4cp/不停的提琴:https://jsfiddle.net/xn9sq4cp/

Fiddle with stop: https://jsfiddle.net/xy18mspz/摆弄停止: 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 JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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