一文詳細(xì)分析Vue3中的emit用法(子傳父)
1. 基本知識(shí)
在 Vue 3 中,emit 是一種機(jī)制,用于在子組件中觸發(fā)事件,并在父組件中監(jiān)聽這些事件

提供一種組件間通信的方式,尤其是在處理父子組件數(shù)據(jù)傳遞和交互時(shí)非常有用
一共有兩種方式
1.1 emit
子組件中使用emit
<template>
<button @click="handleClick">Click me</button>
</template>
<script>
export default {
name: 'ChildComponent',
methods: {
handleClick() {
this.$emit('custom-event', 'Hello from child');
}
}
}
</script>
父組件監(jiān)聽子組件:
<template>
<div>
<ChildComponent @custom-event="handleCustomEvent" />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
name: 'ParentComponent',
components: {
ChildComponent
},
methods: {
handleCustomEvent(payload) {
console.log(payload); // 輸出 'Hello from child'
}
}
}
</script>
1.2 defineEmits
在 Vue 3 中,還可以使用 Composition API 的 defineEmits 方法來定義和使用 emit
子組件中定義和使用emit:
<template>
<button @click="emitEvent">Click me</button>
</template>
<script setup>
import { defineEmits } from 'vue';
const emit = defineEmits(['custom-event']);
function emitEvent() {
emit('custom-event', 'Hello from child with Composition API');
}
</script>
父組件監(jiān)聽子組件:
<template>
<div>
<ChildComponent @custom-event="handleCustomEvent" />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
name: 'ParentComponent',
components: {
ChildComponent
},
methods: {
handleCustomEvent(payload) {
console.log(payload); // 輸出 'Hello from child with Composition API'
}
}
}
</script>
2. Demo
完整Demo如下:
- 創(chuàng)建子組件:
<template>
<button @click="emitEvent">Click me</button>
</template>
<script setup>
import { defineEmits } from 'vue';
const emit = defineEmits(['custom-event']);
function emitEvent() {
emit('custom-event', 'Hello from child with Composition API');
}
</script>
- 創(chuàng)建父組件:
<template>
<div>
<ChildComponent @custom-event="handleCustomEvent" />
<p>{{ message }}</p>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
import { ref } from 'vue';
export default {
name: 'ParentComponent',
components: {
ChildComponent
},
setup() {
const message = ref('');
function handleCustomEvent(payload) {
message.value = payload;
}
return {
message,
handleCustomEvent
};
}
}
</script>
- 應(yīng)用組件:
<template>
<ParentComponent />
</template>
<script>
import ParentComponent from './components/ParentComponent.vue';
export default {
name: 'App',
components: {
ParentComponent
}
}
</script>
主入口文件:
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app');總結(jié)
到此這篇關(guān)于Vue3中的emit用法(子傳父)的文章就介紹到這了,更多相關(guān)Vue3中emit用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的方法
最近項(xiàng)目中需要用到日期選擇器,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的相關(guān)資料,需要的朋友可以參考下2023-09-09
vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Vue2+Elementui?Dialog實(shí)現(xiàn)封裝自定義彈窗組件
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,本文就來為大家詳細(xì)介紹一下Vue2如何結(jié)合Elementui?Dialog實(shí)現(xiàn)封裝自定義彈窗組件,希望對(duì)大家有所幫助2023-12-12
vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式
這篇文章主要介紹了vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決
這篇文章主要介紹了vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue?cli2?和?cli3去掉eslint檢查器報(bào)錯(cuò)的解決
這篇文章主要介紹了vue?cli2?和?cli3去掉eslint檢查器報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

