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/events-api.md
+40-19Lines changed: 40 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -7,77 +7,98 @@ badges:
7
7
8
8
## Overview
9
9
10
-
`$on`, `$off` and `$once` instance methods are removed. Application instances no longer implement the event emitter interface.
10
+
`$on`, `$off` and `$once` instance methods are removed. Component instances no longer implement the event emitter interface.
11
11
12
12
## 2.x Syntax
13
13
14
-
In 2.x, Vue instance could be used to trigger handlers attached imperatively via the event emitter API (`$on`, `$off` and `$once`). This was used to create _event hubs_ to create global event listeners used across the whole application:
14
+
In 2.x, a Vue instance could be used to trigger handlers attached imperatively via the event emitter API (`$on`, `$off` and `$once`). This could be used to create an _event bus_ to create global event listeners used across the whole application:
15
15
16
16
```js
17
-
//eventHub.js
17
+
//eventBus.js
18
18
19
-
consteventHub=newVue()
19
+
consteventBus=newVue()
20
20
21
-
exportdefaulteventHub
21
+
exportdefaulteventBus
22
22
```
23
23
24
24
```js
25
25
// ChildComponent.vue
26
-
importeventHubfrom'./eventHub'
26
+
importeventBusfrom'./eventBus'
27
27
28
28
exportdefault {
29
29
mounted() {
30
-
// adding eventHub listener
31
-
eventHub.$on('custom-event', () => {
30
+
// adding eventBus listener
31
+
eventBus.$on('custom-event', () => {
32
32
console.log('Custom event triggered!')
33
33
})
34
34
},
35
35
beforeDestroy() {
36
-
// removing eventHub listener
37
-
eventHub.$off('custom-event')
36
+
// removing eventBus listener
37
+
eventBus.$off('custom-event')
38
38
}
39
39
}
40
40
```
41
41
42
42
```js
43
43
// ParentComponent.vue
44
-
importeventHubfrom'./eventHub'
44
+
importeventBusfrom'./eventBus'
45
45
46
46
exportdefault {
47
47
methods: {
48
48
callGlobalCustomEvent() {
49
-
eventHub.$emit('custom-event') // if ChildComponent is mounted, we will have a message in the console
49
+
eventBus.$emit('custom-event') // if ChildComponent is mounted, we will have a message in the console
50
50
}
51
51
}
52
52
}
53
53
```
54
54
55
55
## 3.x Update
56
56
57
-
We removed `$on`, `$off` and `$once` methods from the instance completely. `$emit` is still a part of the existing API as it's used to trigger event handlers declaratively attached by a parent component
57
+
We removed `$on`, `$off` and `$once` methods from the instance completely. `$emit` is still a part of the existing API as it's used to trigger event handlers declaratively attached by a parent component.
58
58
59
59
## Migration Strategy
60
60
61
-
In Vue 3, it is no longer possible to use these APIs to listen to a component's own emitted events from within a component, there is no migration path for that use case.
In Vue 3, it is no longer possible to use these APIs to listen to a component's own emitted events from within a component. There is no migration path for that use case.
64
+
65
+
### Root Component Events
66
+
67
+
Static event listeners can be added to the root component by passing them as props to `createApp`:
68
+
69
+
```js
70
+
createApp(App, {
71
+
// Listen for the 'expand' event
72
+
onExpand() {
73
+
console.log('expand')
74
+
}
75
+
})
76
+
```
62
77
63
-
But the eventHub pattern can be replaced by using an external library implementing the event emitter interface, for example [mitt](https://github.com/developit/mitt) or [tiny-emitter](https://github.com/scottcorgan/tiny-emitter).
78
+
### Event Bus
79
+
80
+
The event bus pattern can be replaced by using an external library implementing the event emitter interface, for example [mitt](https://github.com/developit/mitt) or [tiny-emitter](https://github.com/scottcorgan/tiny-emitter).
64
81
65
82
Example:
66
83
67
84
```js
68
-
//eventHub.js
85
+
// eventBus.js
69
86
importemitterfrom'tiny-emitter/instance'
70
87
71
88
exportdefault {
72
89
$on: (...args) =>emitter.on(...args),
73
90
$once: (...args) =>emitter.once(...args),
74
91
$off: (...args) =>emitter.off(...args),
75
-
$emit: (...args) =>emitter.emit(...args),
92
+
$emit: (...args) =>emitter.emit(...args)
76
93
}
77
94
```
78
95
79
96
This provides the same event emitter API as in Vue 2.
80
97
81
-
These methods may also be supported in a future compatibility build of Vue 3.
98
+
In most circumstances, using a global event bus for communicating between components is discouraged. While it is often the simplest solution in the short term, it almost invariably proves to be a maintenance headache in the long term. Depending on the circumstances, there are various alternatives to using an event bus:
*[Props](/guide/component-basics.html#passing-data-to-child-components-with-props) and [events](/guide/component-basics.html#listening-to-child-components-events) should be your first choice for parent-child communication. Siblings can communicate via their parent.
101
+
*[Provide and inject](/guide/component-provide-inject.html) allow a component to communicate with its slot contents. This is useful for tightly-coupled components that are always used together.
102
+
*`provide`/`inject` can also be used for long-distance communication between components. It can help to avoid 'prop drilling', where props need to be passed down through many levels of components that don't need those props themselves.
103
+
* Prop drilling can also be avoided by refactoring to use slots. If an interim component doesn't need the props then it might indicate a problem with separation of concerns. Introducing a slot in that component allows the parent to create the content directly, so that props can be passed without the interim component needing to get involved.
104
+
*[Global state management](/guide/state-management.html), such as [Vuex](https://next.vuex.vuejs.org/).
0 commit comments