一文詳細(xì)分析Vue3中的emit用法(子傳父)
1. 基本知識(shí)
在 Vue 3 中,emit 是一種機(jī)制,用于在子組件中觸發(fā)事件,并在父組件中監(jiān)聽(tīng)這些事件
提供一種組件間通信的方式,尤其是在處理父子組件數(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)聽(tīng)子組件:
<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 方法來(lái)定義和使用 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)聽(tīng)子組件:
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中element-plus表格搜索過(guò)濾數(shù)據(jù)
本文主要介紹了vue3中element-plus表格搜索過(guò)濾數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03vue與electron實(shí)現(xiàn)進(jìn)程間的通信詳情
這篇文章主要介紹了vue與electron實(shí)現(xiàn)進(jìn)程間的通信詳情,本文主要介紹electron渲染進(jìn)程和主進(jìn)程間的通信,以及在渲染進(jìn)程和主進(jìn)程中常用的配置項(xiàng),需要的朋友可以參考一下2022-09-09vue3中<script?setup>?和?setup函數(shù)的區(qū)別對(duì)比
這篇文章主要介紹了vue3中<script?setup>?和?setup函數(shù)的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04vue實(shí)現(xiàn)element表格里表頭信息提示功能(推薦)
小編最近接了這樣一個(gè)需求,需要在element表格操作一欄添加提示功能,下面小編給大家?guī)?lái)了基于vue實(shí)現(xiàn)element表格里表頭信息提示功能,需要的朋友參考下吧2019-11-11vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用)
這篇文章主要介紹了vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用),實(shí)現(xiàn)思路是點(diǎn)擊發(fā)送驗(yàn)證碼,驗(yàn)證碼倒計(jì)時(shí),校驗(yàn)手機(jī)號(hào)是否正常等一系列操作,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性
這篇文章主要介紹了vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12在vue中使用rules對(duì)表單字段進(jìn)行驗(yàn)證方式
這篇文章主要介紹了在vue中使用rules對(duì)表單字段進(jìn)行驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06關(guān)于vue中輸入框的使用場(chǎng)景總結(jié)
這篇文章主要介紹了關(guān)于vue中輸入框的使用場(chǎng)景總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05