Skip to content

Commit 48ea900

Browse files
docs: added events as props and typing with object based emits (#2583)
* docs: added events as props and typing with object based emits * Apply suggestions from code review Co-authored-by: Natalia Tepluhina <[email protected]> * remove info --------- Co-authored-by: Natalia Tepluhina <[email protected]>
1 parent 05a2590 commit 48ea900

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/guide/components/events.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (typeof window !== 'undefined') {
1717
}
1818
}
1919
</script>
20+
2021
# Component Events {#component-events}
2122

2223
> This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
@@ -176,14 +177,14 @@ export default {
176177

177178
</div>
178179

179-
The `emits` option and `defineEmits()` macro also support an object syntax, which allows us to perform runtime validation of the payload of the emitted events:
180+
The `emits` option and `defineEmits()` macro also support an object syntax. If using TypeScript you can type arguments, which allows us to perform runtime validation of the payload of the emitted events:
180181

181182
<div class="composition-api">
182183

183184
```vue
184185
<script setup>
185186
const emit = defineEmits({
186-
submit(payload) {
187+
submit(payload: { email: string, password: string }) {
187188
// return `true` or `false` to indicate
188189
// validation pass / fail
189190
}
@@ -210,7 +211,7 @@ More details: [Typing Component Emits](/guide/typescript/composition-api#typing-
210211
```js
211212
export default {
212213
emits: {
213-
submit(payload) {
214+
submit(payload: { email: string, password: string }) {
214215
// return `true` or `false` to indicate
215216
// validation pass / fail
216217
}
@@ -287,3 +288,14 @@ export default {
287288
```
288289

289290
</div>
291+
292+
## Events as Props {#events-props}
293+
294+
You may also declare and pass `events` as `props`, by prefixing the capitalized event name with `on`
295+
Using `props.onEvent` has a different behaviour than using `emit('event')`, as the former will pass only handle the property based listener (either `@event` or `:on-event`)
296+
297+
:::warning
298+
If both `:onEvent` and `@event` are passed `props.onEvent` might be an array of `functions` instead of `function`, this behavior is not stable and might change in the future.
299+
:::
300+
301+
Because of this, it is recommended to use `emit('event')` instead of `props.onEvent` when emitting events.

src/guide/typescript/composition-api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ In `<script setup>`, the `emit` function can also be typed using either runtime
143143
// runtime
144144
const emit = defineEmits(['change', 'update'])
145145
146+
// options based
147+
const emit = defineEmits({
148+
change: (id: number) => {
149+
// return `true` or `false` to indicate
150+
// validation pass / fail
151+
},
152+
update: (value: string) => {
153+
// return `true` or `false` to indicate
154+
// validation pass / fail
155+
}
156+
})
157+
146158
// type-based
147159
const emit = defineEmits<{
148160
(e: 'change', id: number): void

0 commit comments

Comments
 (0)