From 05a1566d9f6a9ca2004c68c02583b04447c97839 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Sun, 7 Feb 2021 10:01:40 +0100 Subject: [PATCH] migration: add example for replacement of eventHub --- src/guide/migration/events-api.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/guide/migration/events-api.md b/src/guide/migration/events-api.md index ba09f78f73..0e22aeb377 100644 --- a/src/guide/migration/events-api.md +++ b/src/guide/migration/events-api.md @@ -58,6 +58,24 @@ We removed `$on`, `$off` and `$once` methods from the instance completely. `$emi ## Migration Strategy -Existing event hubs 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). +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. -These methods can also be supported in compatibility builds. +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). + +Example: + +```js +//eventHub.js +import emitter from 'tiny-emitter/instance' + +export default { + $on: (...args) => emitter.on(...args), + $once: (...args) => emitter.once(...args), + $off: (...args) => emitter.off(...args), + $emit: (...args) => emitter.emit(...args), +} +``` + +This provides the same event emitter API as in Vue 2. + +These methods may also be supported in a future compatibility build of Vue 3.