首頁 > web前端 > Vue.js > 主體

Vue3兄弟組件傳值之mitt怎麼安裝使用

WBOY
發布: 2023-06-01 19:22:04
轉載
1986 人瀏覽過

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

  • 首先它夠小,只有200bytes。

  • 其次支援全部事件的監聽和批次移除。

  • 它也不依賴 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 發送訊息

<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 &#39;vue&#39;;
import About from &#39;../about/about.vue&#39;

const { appContext } = getCurrentInstance() as ComponentInternalInstance;
const money = ref<number>(98);

const sendMitt = () => {
    appContext.config.globalProperties.$mitt.emit(&#39;moneyEvent&#39;, money.value += 2);
}

</script>

<style lang="less">
</style>
登入後複製

二、在about.vue元件中使用on\color{#ef2d26}{on}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 &#39;vue&#39;;

const amount = ref(0);
const { appContext } = getCurrentInstance() as ComponentInternalInstance;

onMounted(() => {
    appContext.config.globalProperties.$mitt.on(&#39;moneyEvent&#39;, (res: number) => {
        amount.value = res;
    })
})

onBeforeMount(() => {
    appContext.config.globalProperties.$mitt.off(&#39;moneyEvent&#39;);
});

</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 發送訊息

<template>
    <div class="home-container">
        <p>这里是home组件</p>
        <button @click="sendMitt">$mitt发送数据</button>
        <About></About>
    </div>
</template>

<script lang="ts" setup>
import { ref } from &#39;vue&#39;;
import About from &#39;../about/about.vue&#39;
import emitter from &#39;../../utils/bus&#39;

const money = ref<number>(98);

const sendMitt = () => {
    emitter.emit(&#39;moneyEvent&#39;, money.value += 2);
}
</script>

<style lang="less">
</style>
登入後複製

二、在about.vue元件中引入並使用on\color{# ef2d26}{on}on 接收訊息

<template>
    <div class="about-container">
        <p>这里是about组件</p>
        <p>接收到的数据:{{ amount }}</p>
    </div>
</template>

<script lang="ts" setup>
import { ref, onBeforeMount, onMounted } from &#39;vue&#39;;
import emitter from &#39;../../utils/bus&#39;

const amount = ref(0);

onMounted(() => {
    emitter.on(&#39;moneyEvent&#39;, (res: any) => {
        amount.value = res;
    });
})

onBeforeMount(() => {
    emitter.off(&#39;moneyEvent&#39;);
});

</script>

<style lang="less">
.about-container {
    background-color: #f0f0f0;
}
</style>
登入後複製

以上是Vue3兄弟組件傳值之mitt怎麼安裝使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!