Steps for Vue3 to use mitt for component communication

Steps for Vue3 to use mitt for component communication
  • Vue2.x uses EventBus for component communication, while Vue3.x recommends using mitt.js.
  • What is the advantage of mitt.js over EventBus on Vue instances? First of all, it is small enough, only 200 bytes. Secondly, it supports monitoring and batch removal of all events. It does not rely on the Vue instance, so it can be used across frameworks. React or Vue, and even jQuery projects can use the same library.

1. Installation

It 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 main.js

// 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()

/common/EventBus.js : You can also encapsulate an ES module and expose a Mitt instance to the outside world

import mitt from 'mitt'
export default new mitt()

/views/Home.vue : Business module is introduced for use

import EventBus from '/common/EventBus.js'

3. Use

Trigger 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:
  • Detailed explanation of non-parent-child component communication in Vue3
  • Details of 7 kinds of component communication in Vue3
  • Vue3.x uses mitt.js for component communication
  • Summary and examples of vue3 component communication methods

<<:  mysql5.7.21 utf8 encoding problem and solution in Mac environment

>>:  Linux configuration SSH password-free login "ssh-keygen" basic usage

Recommend

How to install MySQL 8.0 and log in to MySQL on MacOS

Follow the official tutorial, download the instal...

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...

Install Centos7 using Hyper-v virtual machine

Table of contents introduce Prepare Download syst...

Nginx configuration to achieve multiple server load balancing

Nginx load balancing server: IP: 192.168.0.4 (Ngi...

A brief discussion on using virtual lists to optimize tables in el-table

Table of contents Preface Solution Specific imple...

A detailed introduction to JavaScript execution mechanism

Table of contents 1. The concept of process and t...

How to solve the slow speed of MySQL Like fuzzy query

Question: Although the index has been created, wh...

How to permanently change the host name in Linux

If you want to change your host name, you can fol...

Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

CSS has two pseudo-classes that are not commonly ...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...

A detailed introduction to HTML page loading and parsing process

The order in which the browser loads and renders H...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...