vue3.x中emits的基本用法實例

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

<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的顯示與隱藏,點擊按鈕,觸發(fā)clickButton函數(shù),父組件調(diào)用,觸發(fā)自定義事件close-modal,而close-modal在父組件中綁定了onModalClose函數(shù),實現(xiàn)了對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: {
// 無需驗證寫法
'close-model': null,
// 這種寫法 自定義函數(shù) 可以在運行時驗證參數(shù)是否正確
'close-modals': (payload: any) => {
return payload.type === 'close'
}
},
setup(props,context) {
const buttonClick = () => {
context.emit('close-model')
}
// 這種寫法來校驗
context.emit('close-modals',{
// 如果驗證失敗會有一個警告
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用async/await來實現(xiàn)同步和異步的案例分享
近期項目中大量使用async,從服務(wù)器獲取數(shù)據(jù),解決一些并發(fā)傳參問題,代碼很簡單,在此也看了一些博客,現(xiàn)在async/await已經(jīng)大范圍讓使用,所以本文給大家介紹一下vue使用async/await來實現(xiàn)同步和異步的案例,需要的朋友可以參考下2024-01-01
vue后臺系統(tǒng)管理項目之角色權(quán)限分配管理功能(示例詳解)
這篇文章主要介紹了vue后臺系統(tǒng)管理項目-角色權(quán)限分配管理功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
vue項目中v-model父子組件通信的實現(xiàn)詳解
vue.js,是一個構(gòu)建數(shù)據(jù)驅(qū)動的 web 界面的庫。Vue.js 的目標是通過盡可能簡單的 API 實現(xiàn)響應的數(shù)據(jù)綁定和組合的視圖組件。下面這篇文章主要給大家介紹了關(guān)于vue項目中v-model父子組件通信實現(xiàn)的相關(guān)資料,需要的朋友可以參考下。2017-12-12
Vue項目中v-model和sync的區(qū)別及使用場景分析
在Vue項目中,v-model和.sync是實現(xiàn)父子組件雙向綁定的兩種方式,v-model主要用于表單元素和子組件的雙向綁定,通過modelValue和update:modelValue實現(xiàn),.sync修飾符則用于同步prop值,適合在子組件內(nèi)更新父組件prop值的場景,通過update:propName事件實現(xiàn)2024-11-11

