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

Detailed steps to install Sogou input method on Ubuntu 20.04

1. Install Fcitx input framework Related dependen...

How to compile and install opencv under ubuntu

Easy installation of opencv2: conda install --cha...

Detailed analysis of the chmod command to modify file permissions under Linux

Use the Linux chmod command to control who can ac...

docker-maven-plugin packages the image and uploads it to a private warehouse

Table of contents 1. Introduction to docker-maven...

Linux IO multiplexing epoll network programming

Preface This chapter uses basic Linux functions a...

Introduction to who command examples in Linux

About who Displays users logged into the system. ...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

MySQL cursor detailed introduction

Table of contents 1. What is a cursor? 2. How to ...

How to make a tar file of wsl through Docker

I've been playing with the remote development...

HTML Tutorial: Collection of commonly used HTML tags (6)

These introduced HTML tags do not necessarily ful...

How to implement MySQL bidirectional backup

MySQL bidirectional backup is also called master-...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...

Detailed explanation of how to implement secondary cache with MySQL and Redis

Redis Introduction Redis is completely open sourc...

JavaScript to achieve digital clock effect

This article example shares the specific code of ...