-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Document changes related to $attrs $listeners and v-on.native (close #526,#592) #608
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
04f80cb
feat: Add migration guide for 3 changes:
426b551
feat: document $attrs new behaviour
f3b66d3
fix: typos, grammar and language improvements
843917e
more fixes
6f1665d
Update src/guide/migration/emits-option.md
LinusBorg e329944
Update src/guide/migration/introduction.md
LinusBorg 4f536ca
Update src/guide/migration/listeners-removed.md
LinusBorg f7cceb9
Update src/guide/migration/introduction.md
LinusBorg 33f0956
Update src/guide/migration/emits-option.md
LinusBorg eefcb59
Update src/guide/migration/listeners-removed.md
LinusBorg 1a3bb75
Update src/guide/migration/listeners-removed.md
LinusBorg 1df13cd
Update src/guide/migration/v-on-native-modifier-removed.md
LinusBorg 4988139
Update src/guide/migration/emits-option.md
LinusBorg 58b5404
Update src/guide/migration/introduction.md
LinusBorg e7b32a3
still more mistakes to fix.
d533308
Merge branch 'linusborg/event-listener-chaanges' of github.com:vuejs/…
2582673
fix link to api docs for "emits:"
4b6292c
use vue instead of html as hightligh lang where possible
4959543
fix broken link
911c32b
fix missing template content
c50eb43
wording/grammar
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
title: $attrs includes class & style | ||
badges: | ||
- breaking | ||
--- | ||
|
||
# `$attrs` includes `class` & `style` <MigrationBadges :badges="$frontmatter.badges" /> | ||
|
||
## Overview | ||
|
||
`$attrs` now contains _all_ attributes passed to a component, including `class` and `style`. | ||
|
||
## 2.x Behavior | ||
|
||
`class` and `style` attributes get some special handling in the Vue 2 virtual DOM implementation. For that reason, they are _not_ included in `$attrs`, while all other attributes are. | ||
|
||
A side effect of this manifests when using `inheritAttrs: false`: | ||
|
||
- Attributes in `$attrs` are no longer automatically added to the root element, leaving it to the developer to decide where to add them. | ||
- But `class` and `style`, not being part of `$attrs`, will still be applied to the component's root element: | ||
|
||
```vue | ||
<template> | ||
<label> | ||
<input type="text" v-bind="$attrs" /> | ||
</label> | ||
</template> | ||
<script> | ||
export default { | ||
inheritAttrs: false | ||
} | ||
</script> | ||
``` | ||
|
||
when used like this: | ||
|
||
```html | ||
<my-component id="my-id" class="my-class"></my-component> | ||
``` | ||
|
||
...will generate this HTML: | ||
|
||
```html | ||
<label class="my-class"> | ||
<input type="text" id="my-id" /> | ||
</label> | ||
``` | ||
|
||
## 3.x Behavior | ||
|
||
`$attrs` contains _all_ attributes, which makes it easier to apply all of them to a different element. The example from above now generates the following HTML: | ||
|
||
```html | ||
<label> | ||
<input type="text" id="my-id" class="my-class" /> | ||
</label> | ||
``` | ||
|
||
## Migration Strategy | ||
|
||
In components that use `inheritAttrs: false`, make sure that styling still works as intended. If you previously relied on the special behavior of `class` and `style`, some visuals might be broken as these attributes might now be applied to another element. | ||
|
||
## See also | ||
|
||
- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0031-attr-fallthrough.md) | ||
- [Migration guide - `$listeners` removed](./listeners-removed.md) | ||
- [Migration guide - New Emits Option](./emits-option.md) | ||
- [Migration guide - `.native` modifier removed](./v-on-native-modifier-removed.md) | ||
- [Migration guide - Changes in the Render Functions API](./render-function-api.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
title: emits Option | ||
badges: | ||
- new | ||
--- | ||
|
||
# `emits` Option <MigrationBadges :badges="$frontmatter.badges" /> | ||
|
||
## Overview | ||
|
||
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. | ||
|
||
## 2.x Behavior | ||
|
||
In Vue 2, you can define the props that a component received, but you can't declare which events it can emit: | ||
|
||
```html | ||
<template> | ||
<div> | ||
<p>{{ text }}</p> | ||
<button v-on:click="$emit('accepted')">OK</button> | ||
</div> | ||
</template> | ||
<script> | ||
export default { | ||
props: ['text'] | ||
} | ||
</script> | ||
``` | ||
|
||
## 3.x Behavior | ||
|
||
Similar to props, the events that the component emits can now be defined with the `emits` option. | ||
|
||
```html | ||
<template> | ||
<p>{{ text }}</p> | ||
<button v-on:click="$emit('accepted')">OK</button> | ||
</template> | ||
<script> | ||
export default { | ||
props: ['text'], | ||
emits: ['accepted'] | ||
} | ||
</script> | ||
``` | ||
|
||
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. | ||
|
||
For more information on this, please read the [API documentation for this feature](../../api/options-data.md#emits). | ||
|
||
## Migration Strategy | ||
|
||
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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The words are not quite in the right order here. I think you were aiming for:
|
||
|
||
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). | ||
|
||
### Example | ||
|
||
For components that re-emit native events to their parent, this would now lead to two events being fired: | ||
|
||
```vue | ||
<template> | ||
<p>{{ text }}</p> | ||
<button v-on:click="$emit('click', $event)">OK</button> | ||
</template> | ||
<script> | ||
export default { | ||
props: ['text'], | ||
LinusBorg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
emits: [] // without declared event | ||
} | ||
</script> | ||
``` | ||
|
||
When a parent listens for the `click` event on the component: | ||
|
||
```html | ||
<my-button v-on:click="handleClick"></my-button> | ||
``` | ||
|
||
it would now be triggered _twice_: | ||
|
||
- Once from `$emit()` | ||
- Once from a native event listener applied to the root element | ||
|
||
Here you have two options: | ||
|
||
1. Properly declare the `click` event. This is useful if you actually do add some logic to that event handler in `<my-button>` | ||
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. | ||
|
||
## See also | ||
|
||
- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md) | ||
- [Migration guide - `.native` modifier removed](./v-on-native-modifier-removed.md) | ||
- [Migration guide - `$listeners` removed](./listeners-removed.md) | ||
- [Migration guide - `$attrs` includes `class` & `style` ](./attrs-includes-class-style.md) | ||
- [Migration guide - Changes in the Render Functions API](./render-function-api.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: $listeners removed | ||
badges: | ||
- breaking | ||
--- | ||
|
||
# `$listeners` removed <MigrationBadges :badges="$frontmatter.badges" /> | ||
|
||
## Overview | ||
|
||
The `$listeners` object has been removed in Vue 3. Event listeners are now part of `$attrs`: | ||
|
||
```javascript | ||
{ | ||
text: 'this is an attribute', | ||
onClose: () => console.log('close Event triggered') | ||
} | ||
``` | ||
|
||
## 2.x Syntax | ||
|
||
In Vue 2, you can access attributes passed to your components with `this.$attrs`, and event listeners with `this.$listeners`. | ||
In combination with `inheritAttrs: false`, they allow the developer to apply these attributes and listeners to some other element instead of the root element: | ||
|
||
```html | ||
LinusBorg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<template> | ||
<label> | ||
<input type="text" v-bind="$attrs" v-on="$listeners" /> | ||
</label> | ||
</template> | ||
<script> | ||
export default { | ||
inheritAttrs: false | ||
} | ||
</script> | ||
``` | ||
|
||
## 3.x Syntax | ||
|
||
In Vue 3's virtual DOM, event listeners are now just attributes, prefixed with `on`, and as such are part of the `$attrs` object, so `$listeners` has been removed. | ||
|
||
```vue | ||
<template> | ||
<label> | ||
<input type="text" v-bind="$attrs" /> | ||
</label> | ||
</template> | ||
<script> | ||
export default { | ||
inheritAttrs: false | ||
} | ||
</script> | ||
``` | ||
|
||
If this component received an `id` attribute and a `v-on:close` listener, the `$attrs` object will now look like this: | ||
|
||
```javascript | ||
{ | ||
id: 'my-input', | ||
onClose: () => console.log('close Event triggered') | ||
} | ||
``` | ||
|
||
## Migration Strategy | ||
|
||
Remove all usages of `$listeners`. | ||
|
||
## See also | ||
|
||
- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0031-attr-fallthrough.md) | ||
- [Migration guide - `$attrs`includes `class` & `style` ](./attrs-includes-class-style.md) | ||
- [Migration guide - Changes in the Render Functions API](./render-function-api.md) | ||
- [Migration guide - New Emits Option](./emits-option.md) | ||
- [Migration guide - `.native` modifier removed](./v-on-native-modifier-removed.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: v-on.native modifier removed | ||
badges: | ||
- breaking | ||
--- | ||
|
||
# `v-on.native` modifier removed <MigrationBadges :badges="$frontmatter.badges" /> | ||
|
||
## Overview | ||
|
||
The `.native` modifier for `v-on` has been removed. | ||
|
||
## 2.x Syntax | ||
|
||
Event listeners passed to a component with `v-on` are by default only triggered by emitting an event with `this.$emit`. To add a native DOM listener to the child component's root element instead, the `.native` modifier can be used: | ||
|
||
```html | ||
<my-component | ||
v-on:close="handleComponentEvent" | ||
v-on:click.native="handleNativeClickEvent" | ||
/> | ||
``` | ||
|
||
## 3.x Syntax | ||
|
||
The `.native` modifier for `v-on` has been removed. At the same time, the [new `emits` option](./emits-option.md) allows the child to define which events it does indeed emit. | ||
|
||
Consequently, Vue will now add all event listeners that are _not_ defined as component-emitted events in the child as native event listeners to the child's root element (unless `inheritAttrs: false` has been set in the child's options). | ||
|
||
```html | ||
<my-component | ||
v-on:close="handleComponentEvent" | ||
v-on:click="handleNativeClickEvent" | ||
/> | ||
``` | ||
|
||
`MyComponent.vue` | ||
|
||
```html | ||
<script> | ||
export default { | ||
emits: ['close'] | ||
} | ||
</script> | ||
``` | ||
|
||
## Migration Strategy | ||
|
||
- remove all instances of the `.native` modifier. | ||
- ensure that all your components document their events with the `emits` option. | ||
|
||
## See also | ||
|
||
- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0031-attr-fallthrough.md#v-on-listener-fallthrough) | ||
- [Migration guide - New Emits Option](./emits-option.md) | ||
- [Migration guide - `$listeners` removed](./listeners-removed.md) | ||
- [Migration guide - Changes in the Render Functions API](./render-function-api.md) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.