Skip to content
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

Events as Props: more details added #2594

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 9 additions & 4 deletions src/guide/components/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,16 @@ export default {

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

Using `props.onSomeEvent` has a different behaviour than using `emit('someEvent')`, as `props.onSomeEvent` gets the callback function declared as a listener in the parent (either `@someEvent` or `:onSomeEvent`).

:::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.
If both `:onSomeEvent` and `@someEvent` are passed, `props.onSomeEvent` might be an array of functions instead of a function, this behavior is not stable and might change in the future.

If no handler is defined, attempting to use `props.onSomeEvent()` as an emitter, will result in a console error.

When passing an event handler as a prop, and kebab-case is used (ie: `:on-some-event`), the handler won't be interpreted when using the `emit('someEvent')` emitter.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reported the issue vuejs/core#9812 and the PR vuejs/core#9813

:::

Because of this, it is recommended to use `emit('event')` instead of `props.onEvent` when emitting events.
Because of this, it is recommended to use `emit('someEvent')` instead of `props.onSomeEvent` when emitting events, and declare event listeners using the event directives as `@someEvent` instead of `:onSomeEvent`.