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
1. Install Fcitx input framework Related dependen...
Easy installation of opencv2: conda install --cha...
Use the Linux chmod command to control who can ac...
Table of contents 1. Introduction to docker-maven...
Preface This chapter uses basic Linux functions a...
About who Displays users logged into the system. ...
Table of contents Preface difficulty Cross-domain...
Table of contents 1. What is a cursor? 2. How to ...
I've been playing with the remote development...
Based on the Vue image magnifier component packag...
These introduced HTML tags do not necessarily ful...
MySQL bidirectional backup is also called master-...
This article introduces 5 ways to solve the 1px b...
Redis Introduction Redis is completely open sourc...
This article example shares the specific code of ...