Heim >Web-Frontend >View.js >So installieren und verwenden Sie mitt, um Werte in der Vue3-Bruderkomponente zu übergeben

So installieren und verwenden Sie mitt, um Werte in der Vue3-Bruderkomponente zu übergeben

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBnach vorne
2023-06-01 19:22:042157Durchsuche

Inwiefern ist mitt.js im Vergleich zu EventBus auf Vue-Instanzen besser?

  • Erstens ist es klein genug, nur 200 Byte.

  • Zweitens unterstützt es die Überwachung aller Ereignisse und die Stapelentfernung.

  • Es ist außerdem nicht auf Vue-Instanzen angewiesen und kann in allen Frameworks React oder Vue verwendet werden, und sogar jQuery-Projekte können dieselben Bibliotheken verwenden.

Mitt im Projekt installieren

npm install --save mitt

Verwendungsmethode eins: im Prototyp deklarieren

1. Registrieren Sie es und mounten Sie es im globalen

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

in main.tscolor{#ef2d26}{main.ts}main .ts 2. Verwenden Sie emitcolor{#ef2d26}{emit}emit in der home.vue-Komponente, um Informationen zu senden

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

2. Verwenden Sie oncolor{#ef2d26}{on}on in der about.vue-Komponente, um Informationen zu empfangen

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

Verwendungsmethode zwei: Referenz in der Komponente

1. Erstellen Sie eine neue bus.tscolor{#ef2d26}{bus.ts}bus.ts-Datei

import mitt from "mitt";
const emiter = mitt();
export default emiter;

2. Einführung und Verwendung von emitcolor{#ef2d26 in der home.vue-Komponente sendet }{emit}emit Informationen

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

2. Führen Sie oncolor{#ef2d26}{on}on in der about.vue-Komponente ein, um Informationen zu empfangen

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

Das obige ist der detaillierte Inhalt vonSo installieren und verwenden Sie mitt, um Werte in der Vue3-Bruderkomponente zu übergeben. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen