Vue3兄弟組件傳值之mitt的超詳細(xì)講解
前言
Vue2.x 使用 EventBus 事件總線進(jìn)行兄弟組件通信,而在Vue3中事件總線模式已經(jīng)被移除,官方建議使用外部的、實(shí)現(xiàn)了事件觸發(fā)器接口的庫,例如 mitt 或 tiny-emitter。
比起 Vue 實(shí)例上的 EventBus,mitt.js 好在哪里呢?
- 首先它足夠小,僅有200bytes。
- 其次支持全部事件的監(jiān)聽和批量移除。
- 它還不依賴 Vue 實(shí)例,可以跨框架使用,React 或者 Vue,甚至 jQuery 項(xiàng)目都能使用同一套庫。
項(xiàng)目中安裝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實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解,有興趣的朋友們可以參考下。2020-05-05
Vue動態(tài)路由路徑重復(fù)及刷新丟失頁面問題的解決
這篇文章主要介紹了Vue動態(tài)路由路徑重復(fù)及刷新丟失頁面問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue-element如何實(shí)現(xiàn)動態(tài)換膚存儲
這篇文章主要介紹了vue-element如何實(shí)現(xiàn)動態(tài)換膚存儲問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
vue使用this.$message不生效的部分原因及解決方案
這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Vue3封裝組件完整實(shí)例(帶回調(diào)事件)
Vue.js已成為現(xiàn)代Web開發(fā)中不可或缺的技術(shù)之一,雖然Vue.js的一些基礎(chǔ)概念和語法比較易學(xué),但深入挖掘Vue.js的核心概念和功能需要更多的實(shí)踐,下面這篇文章主要給大家介紹了關(guān)于Vue3封裝組件(帶回調(diào)事件)的相關(guān)資料,需要的朋友可以參考下2023-06-06
Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別
這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽Enter鍵的方法與區(qū)別的相關(guān)資料,在Vue中我們可以通過監(jiān)聽鍵盤事件來實(shí)現(xiàn)回車鍵切換焦點(diǎn)的功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
vue+node+socket io實(shí)現(xiàn)多人互動并發(fā)布上線全流程
這篇文章主要介紹了vue+node+socket io實(shí)現(xiàn)多人互動并發(fā)布上線全流程,本文給大家提到了socket.io相關(guān)用法概覽及開發(fā)流程,需要的朋友可以參考下2021-09-09

