vue3.x中emits的基本用法實(shí)例

這是官方的文字介紹。emits重要用于組件之間的通信,觸發(fā)自定義事件,傳遞參數(shù)。
下面演示一個(gè)子組件把事件傳遞到父組件,組件間通信的例子。

<template>
<teleport to="#modal">
<div id="center" v-if="isOpen">
<h2>
<slot>this is a modal</slot>
</h2>
<button @click="clickButton">close</button>
</div>
</teleport>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
props: {
isOpen: Boolean,
},
emits: {
'close-modal': null,
},
setup(props, context) {
const clickButton = () => {
context.emit('close-modal');
};
return {
clickButton,
};
},
});
</script>
<style scoped>
#center {
width: 200px;
height: 200px;
border: 2px solid black;
background: white;
position: fixed;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -100px;
}
</style>
isOpen用來表示Modal的顯示與隱藏,點(diǎn)擊按鈕,觸發(fā)clickButton函數(shù),父組件調(diào)用,觸發(fā)自定義事件close-modal,而close-modal在父組件中綁定了onModalClose函數(shù),實(shí)現(xiàn)了對(duì)Modal的隱藏。
<template>
<div id="main">
<modal :isOpen="modalIsOpen" @close-modal="onModalClose">my modal</modal>
<button @click="onModalOpen">Open Modal</button>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import Modal from './components/Modal.vue';
export default defineComponent({
components: { Modal },
name: 'App',
setup(){
const modalIsOpen = ref(false)
const onModalOpen = ()=>{
modalIsOpen.value = true
}
const onModalClose = ()=>{
modalIsOpen.value = false
}
return{
onModalOpen,
onModalClose,
modalIsOpen
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
button {
font-size: 2rem;
}
</style>
附:vue3自定義組件中的emits使用介紹
<template>
<!-- teleport的使用 to屬性渲染到id為 teleport-test 的元素身上 在index.html中-->
<div id="center" v-if="isOpen">
<slot>插槽</slot>
<button @click="buttonClick">關(guān)閉模態(tài)框</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
props:{
isOpen: {
type: Boolean,
required: true
},
},
// emits 寫自定義事件 作用 比較清晰知道該組件有那些自定義事件
emits: {
// 無需驗(yàn)證寫法
'close-model': null,
// 這種寫法 自定義函數(shù) 可以在運(yùn)行時(shí)驗(yàn)證參數(shù)是否正確
'close-modals': (payload: any) => {
return payload.type === 'close'
}
},
setup(props,context) {
const buttonClick = () => {
context.emit('close-model')
}
// 這種寫法來校驗(yàn)
context.emit('close-modals',{
// 如果驗(yàn)證失敗會(huì)有一個(gè)警告
type: 'close'
// type: 'sss'
})
return {
buttonClick
}
}
})
</script>
<style>
#center{
width: 600px;
height: 300px;
border: 1px solid #999;
background-color: #fff;
position: fixed;
left: 50%;
top: 50%;
margin-left: -300px;
margin-top: -150px;
}
</style>
總結(jié)
到此這篇關(guān)于vue3.x中emits基本用法的文章就介紹到這了,更多相關(guān)vue3.x中emits用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07
vue使用async/await來實(shí)現(xiàn)同步和異步的案例分享
近期項(xiàng)目中大量使用async,從服務(wù)器獲取數(shù)據(jù),解決一些并發(fā)傳參問題,代碼很簡(jiǎn)單,在此也看了一些博客,現(xiàn)在async/await已經(jīng)大范圍讓使用,所以本文給大家介紹一下vue使用async/await來實(shí)現(xiàn)同步和異步的案例,需要的朋友可以參考下2024-01-01
vue后臺(tái)系統(tǒng)管理項(xiàng)目之角色權(quán)限分配管理功能(示例詳解)
這篇文章主要介紹了vue后臺(tái)系統(tǒng)管理項(xiàng)目-角色權(quán)限分配管理功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
vue項(xiàng)目中v-model父子組件通信的實(shí)現(xiàn)詳解
vue.js,是一個(gè)構(gòu)建數(shù)據(jù)驅(qū)動(dòng)的 web 界面的庫(kù)。Vue.js 的目標(biāo)是通過盡可能簡(jiǎn)單的 API 實(shí)現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件。下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中v-model父子組件通信實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下。2017-12-12
Vue項(xiàng)目中v-model和sync的區(qū)別及使用場(chǎng)景分析
在Vue項(xiàng)目中,v-model和.sync是實(shí)現(xiàn)父子組件雙向綁定的兩種方式,v-model主要用于表單元素和子組件的雙向綁定,通過modelValue和update:modelValue實(shí)現(xiàn),.sync修飾符則用于同步prop值,適合在子組件內(nèi)更新父組件prop值的場(chǎng)景,通過update:propName事件實(shí)現(xiàn)2024-11-11
vue項(xiàng)目打包以及優(yōu)化的實(shí)現(xiàn)步驟
項(xiàng)目完成,我們會(huì)將項(xiàng)目進(jìn)行上線,為了提升性能,我們往往會(huì)進(jìn)行一些優(yōu)化處理,本文主要介紹了vue項(xiàng)目打包以及優(yōu)化的實(shí)現(xiàn)步驟,感興趣的可以了解一下2021-07-07
vue指令v-html使用過濾器filters功能實(shí)例
在本篇文章里我們給大家整理的是關(guān)于vue指令v-html使用過濾器filters功能的實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10

