Home > Article > Web Front-end > How to install and use mitt to pass value in Vue3 brother component
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.
npm install --save mitt
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>
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!