1. InstallationIt is recommended to use yarn installation (everyone who has used it knows how smooth it is) yarn add mitt Or install via npm npm install --save mitt 2. Import into the project and mount Can be mounted globally in // Standard ES modular import method import mitt from 'mitt' const app = createApp(App) // The global instance of vue3.x should be mounted on config.globalProperties app.config.globalProperties.$EventBus = new mitt()
import mitt from 'mitt' export default new mitt()
import EventBus from '/common/EventBus.js' 3. UseTrigger via on listener/emit /* * App.vue */ // There is no this in setup, you need to use getCurrentInstance to get the Vue instance import { getCurrentInstance } from 'vue' import { Mp3Player } from '/common/Mp3Player.js' export default { setup(){ // ctx is equivalent to this in Vue2.x const { ctx } = getCurrentInstance() // Listen - if there is a new task, play the sound effect ctx.$EventBus.on('newTask', data => { Mp3Player.play() }) // You can also listen to all tasks through *ctx.$EventBus.on('*', data => { console.log('EventBus come in', data) }) } } /* * Control.vue */ // When a new task is detected, trigger ctx.$EventBus.emit('newTask', data) off remove event import { onBeforeUnmount, getCurrentInstance } from 'vue' export default { setup(){ const { ctx } = getCurrentInstance() onBeforeUnmount(() => { // Remove the specified event ctx.$EventBus.off('newTask') // Remove all events ctx.$EventBus.all.clear() }) } } The above are the details of the steps for Vue3 to use mitt for component communication. For more information about Vue3 using mitt for component communication, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: mysql5.7.21 utf8 encoding problem and solution in Mac environment
>>: Linux configuration SSH password-free login "ssh-keygen" basic usage
Follow the official tutorial, download the instal...
Table of contents 1. Download JDK (take jdk1.8.0 ...
Previously, https://www.jb51.net/article/205922.h...
Table of contents introduce Prepare Download syst...
Nginx load balancing server: IP: 192.168.0.4 (Ngi...
Table of contents Preface Solution Specific imple...
Table of contents 1. The concept of process and t...
Question: Although the index has been created, wh...
If you want to change your host name, you can fol...
Note: sg11 Our company only supports self-install...
CSS has two pseudo-classes that are not commonly ...
Syn attack is the most common and most easily exp...
Table of contents The first step is to download M...
The order in which the browser loads and renders H...
In order to facilitate the storage and access of ...