vue3.x中emits的基本用法實(shí)例
這是官方的文字介紹。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的顯示與隱藏,點(diǎn)擊按鈕,觸發(fā)clickButton函數(shù),父組件調(diào)用,觸發(fā)自定義事件close-modal,而close-modal在父組件中綁定了onModalClose函數(shù),實(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: { // 無需驗(yàn)證寫法 'close-model': null, // 這種寫法 自定義函數(shù) 可以在運(yùn)行時驗(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)證失敗會有一個警告 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組件傳遞對象中實(shí)現(xiàn)單向綁定的示例
下面小編就為大家分享一篇vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue數(shù)據(jù)雙向綁定原理解析(get & set)
這篇文章主要為大家詳細(xì)解析了vue.js數(shù)據(jù)雙向綁定原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Vue+Websocket簡單實(shí)現(xiàn)聊天功能
這篇文章主要為大家詳細(xì)介紹了Vue+Websocket簡單實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08element使用自定義icon圖標(biāo)的詳細(xì)步驟
前端經(jīng)常會用到UI提供的各種圖表,推薦阿里的圖標(biāo)庫,下面這篇文章主要給大家介紹了關(guān)于element使用自定義icon圖標(biāo)的詳細(xì)步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vue router中mode history、base的作用說明
這篇文章主要介紹了vue router中mode history、base的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05