Skip to content

Commit 8b326d5

Browse files
bencodezenphanan
andauthored
docs (#87): document new events emits option (#88)
* docs (#87): remove repeated starting sentence phrase * docs (#87): add new emits option with validation * fix (#87): incorrect code syntax * docs: improve phrasing Co-authored-by: Phan An <[email protected]> Co-authored-by: Phan An <[email protected]>
1 parent ac95807 commit 8b326d5

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/guide/component-custom-events.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,56 @@ Listening to the kebab-cased version will have no effect:
1717
<my-component v-on:my-event="doSomething"></my-component>
1818
```
1919

20-
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.
2121

2222
For these reasons, we recommend you **always use kebab-case for event names**.
2323

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+
return true
56+
} else {
57+
console.warn('Invalid submit event payload!')
58+
return false
59+
}
60+
}
61+
},
62+
methods: {
63+
submitForm() {
64+
this.$emit('submit', { email, password })
65+
}
66+
}
67+
})
68+
```
69+
2470
## `v-model` arguments
2571

2672
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

Comments
 (0)