Vue3一兄弟组件传值之mitt,超详细!

13,217 阅读1分钟

Vue2.x 使用 EventBus 事件总线进行兄弟组件通信,而在Vue3中事件总线模式已经被移除,官方建议使用外部的、实现了事件触发器接口的库,例如 mitt 或 tiny-emitter

比起 Vue 实例上的 EventBus,mitt.js 好在哪里呢?

  1. 首先它足够小,仅有200bytes。
  2. 其次支持全部事件的监听和批量移除。
  3. 它还不依赖 Vue 实例,可以跨框架使用,React 或者 Vue,甚至 jQuery 项目都能使用同一套库。

项目中安装mitt

npm install --save mitt

使用方式一:在原型中声明

一、在 main.ts\color{#ef2d26}{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} 发送信息

<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>

二、在about.vue组件中使用 on\color{#ef2d26}{on} 接收信息

<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>

使用方式二:在组件中引用

一、新建 bus.ts\color{#ef2d26}{bus.ts} 文件

import mitt from "mitt";

const emiter = mitt();

export default emiter;

二、在home.vue组件中引入并使用 emit\color{#ef2d26}{emit} 发送信息

<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>

二、在about.vue组件中引入并使用 on\color{#ef2d26}{on} 接收信息

<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>