1.0.21
https://jsfiddle.net/paulpflug/5sH6A/312/
// in component
this.$emit("testTest") // camelCase
// outside
<comp @test-test="test"></comp>
Event should be called
Event isn't called
This is a bit delicate.
When working with attributes it is totally clear to expect the same behavior as props.
current behavior:
| case | $emit | $on in code | v-on in attributes |
| --- | --- | --- | --- |
| 1 | camelCase | camelCase | not possible |
| 2 | lowercase | lowercase | lowercase /camelCase |
| 3 | hypenated | hypenated | hypenated |
I think this makes more sense:
| case | $emit | $on in code | v-on in attributes |
| --- | --- | --- | --- |
| 1 | camelCase | camelCase | hypenated |
| 2 | lowercase | lowercase | lowercase /camelCase |
| 3 | warn to use camelCase | warn to use camelCase | hypenated |
The current suggestion is simply avoid using camelCase event names, because there's no particular reason to do it.
In my app I followed hook:created syntax making it something like this origincomponent:eventname. Vanilla JS events are mostly all lowercase so I guess it makes sense to stick to that solution in Vue
I have three arguments:
events:
someEvent: -> # allowed without qoutes
<comp :some-prop="someThing"></comp>
# vs.
<comp @some-event="someThing"></comp>
props:
someProp: {}
# vs.
events:
someEvent: -> # only that this currently won't work
v-onfwiw, I'm kind of 50/50 on this.
thoughts:
js
Vue.extend({
events: {
'some-event'(...args) { ... }, // -1
someEvent(...args) { ... }, // +1
});
The quoted & dasherized handler name is kind of _meh_
Readability. It helps to distinguish events from methods. And usually, events are more human readable: 'comment-deleted', 'user-logged-in', 'event-happened'.
@azamat-sharapov - that's a good point. 'some-event'() {} still looks a little awkward, but it definitely does help distinguish event handlers from regular methods.
I oppose, the usage pattern for events and methods is totally different. I can think of no way to confuse the both.
Wherever I can, I use the same names intentionally:
events: {
doStuff: doStuff
}
// or
this.$on('doStuff',this.doStuff)
// both look super clean to me..
I usually try to emphasize the difference between events and methods. An event doesn't necessarily carry any information about what is needed to react to it. For example, a cancel button of a form within an overlay dispatches an event that I'd call overlay-form:cancel-requested.
Maybe I'd like to close the overlay, as the user doesn't intend to fill out this form right now, but mabye the form is mandatory and he needs to fill it.
I'm aware that there are hundreds of ways to do this, but for me it works by separating information about actions into events (the next step was requested, a cancel was requested) and the resulting orders into methods (react to the request by doing xy). It shifts the intent to react from the informational event (sth. happened) to the method that handles it (what do I do now).
So to conclude:
warn to use camelCase as I suggestedv-on directiveanother suggestion:
let v-on bind to someEvent and some-event when a hypenated form is detected.
Difficulty would be, when someEvent and some-event actually mean different things, how likely is that? Is taking that risk better or worse of not supporting camelCase at all?
Most helpful comment
The current suggestion is simply avoid using camelCase event names, because there's no particular reason to do it.