Skip to content

docs: added events as props and typing with object based emits #2583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/guide/components/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (typeof window !== 'undefined') {
}
}
</script>

# Component Events {#component-events}

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

</div>

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:
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:

<div class="composition-api">

```vue
<script setup>
const emit = defineEmits({
submit(payload) {
submit(payload: { email: string, password: string }) {
// return `true` or `false` to indicate
// validation pass / fail
}
Expand All @@ -210,7 +211,7 @@ More details: [Typing Component Emits](/guide/typescript/composition-api#typing-
```js
export default {
emits: {
submit(payload) {
submit(payload: { email: string, password: string }) {
// return `true` or `false` to indicate
// validation pass / fail
}
Expand Down Expand Up @@ -287,3 +288,14 @@ export default {
```

</div>

## Events as Props {#events-props}

You may also declare and pass `events` as `props`, by prefixing the capitalized event name with `on`
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`)

:::warning
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.
:::

Because of this, it is recommended to use `emit('event')` instead of `props.onEvent` when emitting events.
12 changes: 12 additions & 0 deletions src/guide/typescript/composition-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ In `<script setup>`, the `emit` function can also be typed using either runtime
// runtime
const emit = defineEmits(['change', 'update'])

// options based
const emit = defineEmits({
change: (id: number) => {
// return `true` or `false` to indicate
// validation pass / fail
},
update: (value: string) => {
// return `true` or `false` to indicate
// validation pass / fail
}
})

// type-based
const emit = defineEmits<{
(e: 'change', id: number): void
Expand Down