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

Two ways to introduce svg icons in Vue

How to introduce svg icons in Vue Method 1 of int...

CentOS 6 uses Docker to deploy Zookeeper operation example

This article describes how to use docker to deplo...

Vuex implements simple shopping cart function

This article example shares the specific code of ...

Vue implements graphic verification code login

This article example shares the specific code of ...

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Simple principles for web page layout design

This article summarizes some simple principles of...

CSS specification BEM CSS and OOCSS sample code detailed explanation

Preface During project development, due to differ...

Example of using Docker Swarm to build a distributed crawler cluster

During the crawler development process, you must ...

Nodejs converts JSON string into JSON object error solution

How to convert a JSON string into a JSON object? ...

Three ways to check whether a port is open in a remote Linux system

This is a very important topic, not only for Linu...

Tutorial on using the frameset tag in HTML

Frameset pages are somewhat different from ordina...