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
Copy file name to clipboardExpand all lines: src/guide/migration/emits-option.md
+16-16
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,13 @@ badges:
8
8
9
9
## Overview
10
10
11
-
Vue 3 now offers an `emits` option similar to the existing `props` option. This option can be used to define the events that a component can emit to its parent.
11
+
Vue 3 now offers an `emits` option, similar to the existing `props` option. This option can be used to define the events that a component can emit to its parent.
12
12
13
13
## 2.x Behavior
14
14
15
-
In Vue 2, you can define the props that a component received, but you can't declare which events it can emit:
15
+
In Vue 2, you can define the props that a component receives, but you can't declare which events it can emit:
16
16
17
-
```html
17
+
```vue
18
18
<template>
19
19
<div>
20
20
<p>{{ text }}</p>
@@ -30,12 +30,14 @@ In Vue 2, you can define the props that a component received, but you can't decl
30
30
31
31
## 3.x Behavior
32
32
33
-
Similar to props, the events that the component emits can now be defined with the `emits` option.
33
+
Similar to props, the events that the component emits can now be defined with the `emits` option:
@@ -45,28 +47,26 @@ Similar to props, the events that the component emits can now be defined with th
45
47
</script>
46
48
```
47
49
48
-
The option also accepts an object notation, which allows the developer to define validators for the arguments that are passed with the emitted event, similar to validators in props definitions.
50
+
The option also accepts an object, which allows the developer to define validators for the arguments that are passed with the emitted event, similar to validators in `props` definitions.
49
51
50
52
For more information on this, please read the [API documentation for this feature](../../api/options-data.md#emits).
51
53
52
54
## Migration Strategy
53
55
54
-
It is highly recommended that you document all of the emitted events by your each of components this way because of the [removal of the `.native` modifier](./v-on-native-modifier-removed.md).
56
+
It is highly recommended that you document all of the events emitted by each of your components using `emits`.
55
57
56
-
All events not defined with `emits`are now added as DOM event listeners to the component's root node (unless `inheritAttrs: false` has been set).
58
+
This is especially important because of [the removal of the `.native` modifier](./v-on-native-modifier-removed.md). Any listeners for events that aren't declared with `emits`will now be included in the component's `$attrs`, which by default will be bound to the component's root node.
57
59
58
60
### Example
59
61
60
62
For components that re-emit native events to their parent, this would now lead to two events being fired:
@@ -80,18 +80,18 @@ When a parent listens for the `click` event on the component:
80
80
81
81
it would now be triggered _twice_:
82
82
83
-
- Once from `$emit()`
84
-
- Once from a native event listener applied to the root element
83
+
- Once from `$emit()`.
84
+
- Once from a native event listener applied to the root element.
85
85
86
86
Here you have two options:
87
87
88
-
1. Properly declare the `click` event. This is useful if you actually do add some logic to that event handler in `<my-button>`
88
+
1. Properly declare the `click` event. This is useful if you actually do add some logic to that event handler in `<my-button>`.
89
89
2. Remove the re-emitting of the event, since the parent can now listen for the native event easily, without adding `.native`. Suitable when you really only re-emit the event anyway.
0 commit comments