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
I won’t talk about the use of SSL certificates. F...
How to introduce svg icons in Vue Method 1 of int...
Table of contents 1. JavaScript is single-threade...
This article describes how to use docker to deplo...
This article example shares the specific code of ...
This article example shares the specific code of ...
How to modify the mysql table partitioning progra...
Problem: The null type data returned by mybatis d...
Preface: In the previous article, we mainly intro...
This article summarizes some simple principles of...
Preface During project development, due to differ...
During the crawler development process, you must ...
How to convert a JSON string into a JSON object? ...
This is a very important topic, not only for Linu...
Frameset pages are somewhat different from ordina...