I have two components, one contains another.
And when I trigger event from child I can't receive it in parent.
Child component
this.$emit('myCustomEvent', this.data);
Parent component
<parent-component v-on:myCustomEvent="doSomething"></parent-component>
But, when I changed event name to my-custom-event in both places it works.
Vue somehow transform event names? Or what can be a problem? I read docs about component naming convention but there nothing related to event naming
It is recommended to always use kebab-case
for the naming of custom events. Lower case events, all smashed together, as recommended by @KoriJohnRoys would also work but are harder to read. It is not recommended to use camelCase
for event naming.
The official documentation of Vue.JS states the following under the topic of Event Names:
Event Names
Unlike components and props, event names don’t provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event. For example, if emitting a camelCased event name:
this.$emit('myEvent')
Listening to the kebab-cased version will have no effect:
<my-component v-on:my-event="doSomething"></my-component>
Unlike components and props, event names will never be used as variable or property names in JavaScript, so there’s no reason to use camelCase or PascalCase. Additionally, v-on event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML’s case-insensitivity), so v-on:myEvent would become v-on:myevent – making myEvent impossible to listen to.
For these reasons, we recommend you always use kebab-case for event names.