Vue3兄弟組件傳值之mitt的超詳細講解
前言
Vue2.x 使用 EventBus 事件總線進行兄弟組件通信,而在Vue3中事件總線模式已經(jīng)被移除,官方建議使用外部的、實現(xiàn)了事件觸發(fā)器接口的庫,例如 mitt 或 tiny-emitter。
比起 Vue 實例上的 EventBus,mitt.js 好在哪里呢?
- 首先它足夠小,僅有200bytes。
- 其次支持全部事件的監(jiān)聽和批量移除。
- 它還不依賴 Vue 實例,可以跨框架使用,React 或者 Vue,甚至 jQuery 項目都能使用同一套庫。
項目中安裝mitt
npm install --save mitt
使用方式一:在原型中聲明
一、在 main.ts\color{#ef2d26}{main.ts}main.ts 中注冊掛載到全局
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')
二、在home.vue組件中使用 emit\color{#ef2d26}{emit}emit 發(fā)送信息
<template> <div class="home-container"> <p>這里是home組件</p> <button @click="sendMitt">$mitt發(fā)送數(shù)據(jù)</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>
二、在about.vue組件中使用 on\color{#ef2d26}{on}on 接收信息
<template> <div class="about-container"> <p>這里是about組件</p> <p>接收到的數(shù)據(jù):{{ 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>
使用方式二:在組件中引用
一、新建 bus.ts\color{#ef2d26}{bus.ts}bus.ts 文件
import mitt from "mitt"; const emiter = mitt(); export default emiter;
二、在home.vue組件中引入并使用 emit\color{#ef2d26}{emit}emit 發(fā)送信息
<template> <div class="home-container"> <p>這里是home組件</p> <button @click="sendMitt">$mitt發(fā)送數(shù)據(jù)</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>
二、在about.vue組件中引入并使用 on\color{#ef2d26}{on}on 接收信息
<template> <div class="about-container"> <p>這里是about組件</p> <p>接收到的數(shù)據(jù):{{ 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>
總結(jié)
到此這篇關(guān)于Vue3兄弟組件傳值之mitt的文章就介紹到這了,更多相關(guān)Vue3兄弟組件傳值mitt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-element如何實現(xiàn)動態(tài)換膚存儲
這篇文章主要介紹了vue-element如何實現(xiàn)動態(tài)換膚存儲問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue使用this.$message不生效的部分原因及解決方案
這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別
這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽Enter鍵的方法與區(qū)別的相關(guān)資料,在Vue中我們可以通過監(jiān)聽鍵盤事件來實現(xiàn)回車鍵切換焦點的功能,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程
這篇文章主要介紹了vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程,本文給大家提到了socket.io相關(guān)用法概覽及開發(fā)流程,需要的朋友可以參考下2021-09-09