Compared with EventBus on Vue instances, what is the advantage of mitt.js?
First of all, it is small enough, only 200bytes.
Secondly, it supports monitoring and batch removal of all events.
It also does not rely on Vue instances and can be used across frameworks. React or Vue, and even jQuery projects can use the same set of libraries.
Install mitt in the project
npm install --save mitt
Usage method one: declare in the prototype
1. In main.ts\color{# Register in ef2d26}{main.ts}main.ts and mount it to the global
import { createApp } from 'vue' import App from './App.vue' import mitt from 'mitt' import router from "./router"; const app = createApp(App) // vue3挂载到全局 app.config.globalProperties.$mitt = mitt(); app.use(router).mount('#app')
2. Use emit\color{#ef2d26}{emit}emit in the home.vue component to send Information
<template> <div class="home-container"> <p>这里是home组件</p> <button @click="sendMitt">$mitt发送数据</button> <About></About> </div> </template> <script lang="ts" setup> import { getCurrentInstance, ref, ComponentInternalInstance } from 'vue'; import About from '../about/about.vue' const { appContext } = getCurrentInstance() as ComponentInternalInstance; const money = ref<number>(98); const sendMitt = () => { appContext.config.globalProperties.$mitt.emit('moneyEvent', money.value += 2); } </script> <style lang="less"> </style>
2. Use on\color{#ef2d26}{on}on in the about.vue component to receive information
<template> <div class="about-container"> <p>这里是about组件</p> <p>接收到的数据:{{ amount }}</p> </div> </template> <script lang="ts" setup> import { ref, getCurrentInstance, ComponentInternalInstance, onBeforeMount, onMounted } from 'vue'; const amount = ref(0); const { appContext } = getCurrentInstance() as ComponentInternalInstance; onMounted(() => { appContext.config.globalProperties.$mitt.on('moneyEvent', (res: number) => { amount.value = res; }) }) onBeforeMount(() => { appContext.config.globalProperties.$mitt.off('moneyEvent'); }); </script> <style lang="less"> .about-container { background-color: #f0f0f0; } </style>
Usage method two: Reference in the component
1. Create a new bus.ts\color{#ef2d26}{bus.ts}bus.ts file
import mitt from "mitt"; const emiter = mitt(); export default emiter;
2. Introduce and use emit\color{#ef2d26}{emit}emit in the home.vue component to send information
<template> <div class="home-container"> <p>这里是home组件</p> <button @click="sendMitt">$mitt发送数据</button> <About></About> </div> </template> <script lang="ts" setup> import { ref } from 'vue'; import About from '../about/about.vue' import emitter from '../../utils/bus' const money = ref<number>(98); const sendMitt = () => { emitter.emit('moneyEvent', money.value += 2); } </script> <style lang="less"> </style>
2. Introduce and use on\color{# in the about.vue component ef2d26}{on}on receive information
<template> <div class="about-container"> <p>这里是about组件</p> <p>接收到的数据:{{ amount }}</p> </div> </template> <script lang="ts" setup> import { ref, onBeforeMount, onMounted } from 'vue'; import emitter from '../../utils/bus' const amount = ref(0); onMounted(() => { emitter.on('moneyEvent', (res: any) => { amount.value = res; }); }) onBeforeMount(() => { emitter.off('moneyEvent'); }); </script> <style lang="less"> .about-container { background-color: #f0f0f0; } </style>
The above is the detailed content of How to install and use mitt to pass value in Vue3 brother component. For more information, please follow other related articles on the PHP Chinese website!

前端有没有现成的库,可以直接用来绘制 Flowable 流程图的?下面本篇文章就跟小伙伴们介绍一下这两个可以绘制 Flowable 流程图的前端库。

vue不是前端css框架,而是前端JavaScript框架。Vue是一套用于构建用户界面的渐进式JS框架,是基于MVVM设计模式的前端框架,且专注于View层。Vue.js的优点:1、体积小;2、基于虚拟DOM,有更高的运行效率;3、双向数据绑定,让开发者不用再去操作DOM对象,把更多的精力投入到业务逻辑上;4、生态丰富、学习成本低。

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章我们来了解 Vue2.X 响应式原理,然后我们来实现一个 vue 响应式原理(写的内容简单)实现步骤和注释写的很清晰,大家有兴趣可以耐心观看,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools