Skip to content

Commit 00f1c74

Browse files
committed
feat: add migration guide > listeners removed
1 parent 1cc673a commit 00f1c74

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const sidebar = {
128128
'migration/global-api-treeshaking',
129129
'migration/inline-template-attribute',
130130
'migration/keycode-modifiers',
131+
'migration/listeners-removed',
131132
'migration/props-default-this',
132133
'migration/render-function-api',
133134
'migration/slots-unification',
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: $listeners removed
3+
badges:
4+
- breaking
5+
---
6+
7+
# `$listeners` removed <MigrationBadges :badges="$frontmatter.badges" />
8+
9+
## Overview
10+
11+
The `$listeners` object has been removed in Vue 3. Event listeners are now part of `$attrs`:
12+
13+
```javascript
14+
{
15+
text: 'this is an attribute',
16+
onClose: () => console.log('close Event triggered')
17+
}
18+
```
19+
20+
## 2.x Syntax
21+
22+
In Vue 2, you can access attributes passed to your components with `this.$attrs`, and event listeners with `this.$listeners`.
23+
In combination with `inheritAttrs: false`, they allow the developer to apply these attributes and listeners to some other element instead of the root element:
24+
25+
```html
26+
<template>
27+
<label>
28+
<input type="text" v-bind="$attrs" v-on="$listeners" />
29+
</label>
30+
</template>
31+
<script>
32+
export default {
33+
inheritAttrs: false
34+
}
35+
</script>
36+
```
37+
38+
## 3.x Syntax
39+
40+
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.
41+
42+
```vue
43+
<template>
44+
<label>
45+
<input type="text" v-bind="$attrs" />
46+
</label>
47+
</template>
48+
<script>
49+
export default {
50+
inheritAttrs: false
51+
}
52+
</script>
53+
```
54+
55+
If this component received an `id` attribute and a `v-on:close` listener, the `$attrs` object will now look like this:
56+
57+
```javascript
58+
{
59+
id: 'my-input',
60+
onClose: () => console.log('close Event triggered')
61+
}
62+
```
63+
64+
## Migration Strategy
65+
66+
Remove all usages of `$listeners`.
67+
68+
## See also
69+
70+
- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0031-attr-fallthrough.md)
71+
- [Migration guide - `$attrs`includes `class` & `style` ](./attrs-includes-class-style.md)
72+
- [Migration guide - Changes in the Render Functions API](./render-function-api.md)
73+
- [Migration guide - New Emits Option](./emits-option.md)
74+
- [Migration guide - `.native` modifier removed](./v-on-native-modifier-removed.md)

0 commit comments

Comments
 (0)