You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
20
+
Since event names will never be used as variable or property names in JavaScript, there is 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.
21
21
22
22
For these reasons, we recommend you **always use kebab-case for event names**.
23
23
24
+
## Defining Custom Events
25
+
26
+
Emitted events can be defined on the component via the `emits` option.
27
+
28
+
```js
29
+
app.component('custom-form', {
30
+
emits: ['in-focus', 'submit']
31
+
})
32
+
```
33
+
34
+
In the event a native event (e.g., `click`) is defined in the `emits` option, it will be overwritten by the event in the component instead of being treated as a native listener.
35
+
36
+
::: tip
37
+
It is recommended to define all emitted events in order to better document how a component should work.
38
+
:::
39
+
40
+
### Validate Emitted Events
41
+
42
+
Similar to prop type validation, an emitted event can be validated if it is defined with the Object syntax instead of the Array syntax.
43
+
44
+
To add validation, the event is assigned a function that receives the arguments passed to the `$emit` call and returns a boolean to indicate whether the event is valid or not.
45
+
46
+
```js
47
+
app.component('custom-form', {
48
+
emits: {
49
+
// No validation
50
+
click:null,
51
+
52
+
// Validate submit event
53
+
submit: ({ email, password }) => {
54
+
if (email && password) {
55
+
returntrue
56
+
} else {
57
+
console.warn('Invalid submit event payload!')
58
+
returnfalse
59
+
}
60
+
}
61
+
},
62
+
methods: {
63
+
submitForm() {
64
+
this.$emit('submit', { email, password })
65
+
}
66
+
}
67
+
})
68
+
```
69
+
24
70
## `v-model` arguments
25
71
26
72
By default, `v-model` on a component uses `modelValue` as the prop and `update:modelValue` as the event. We can modify these names passing an argument to `v-model`:
0 commit comments