vue3?setup語法糖之組件傳參(defineProps、defineEmits、defineExpose)示例詳解
defineProps
和defineEmits
都是只能在<script setup>
中使用的編譯器宏。他們不需要導(dǎo)入,且會(huì)隨著<script setup>
的處理過程一同被編譯掉。defineProps
接收與props
選項(xiàng)相同的值,defineEmits
接收與emits
選項(xiàng)相同的值。
父傳子 - defineProps
父組件
<template> <div class="Father"> <p>我是父組件</p> <!-- --> <son :ftext="ftext"></son> </div> </template> <script setup> import {ref} from 'vue' import Son from './son.vue' const ftext = ref('我是父組件-text') </script>
子組件
<template> <div class="Son"> <p>我是子組件</p> <!-- 展示來自父組件的值 --> <p>接收到的值:{{ftext}}</p> </div> </template> <script setup> import {ref} from 'vue' // setup 語法糖寫法 //defineProps 來接收組件的傳值 const props = defineProps({ ftext: { type:String }, }) </script>
子傳父 - defineEmits
子組件:
<template> <div class="Son"> <p>我是子組件</p> <button @click="toValue">點(diǎn)擊給父組件傳值</button> </div> </template> <script setup> import {ref} from 'vue' // setup 語法糖寫法 //用defineEmits()來定義子組件要拋出的方法,語法defineEmits(['要拋出的方法']) const emit = defineEmits(['exposeData']) const stext = ref('我是子組件的值-ftext') const toValue = ()=>{ emit('exposeData',stext) } </script>
父組件:
<template> <div class="Father"> <p>我是父組件</p> <!-- --> <son @exposeData="getData" :ftext="ftext"></son> </div> </template> <script setup> import {ref} from 'vue' import Son from './son.vue' const ftext = ref('我是父組件-text') const getData = (val)=>{ console.log("接收子組件的值",val) } </script>
defineExpose
使用 <script setup>
的組件是默認(rèn)關(guān)閉的(即通過模板引用或者 $parent
鏈獲取到的組件的公開實(shí)例,不會(huì)暴露任何在 <script setup>
中聲明的綁定)。
可以通過 defineExpose
編譯器宏來顯式指定在 <script setup>
組件中要暴露出去的屬性
子組件:
<template> <div> <p>我是子組件</p> </div> </template> <script setup> import { ref } from 'vue'; const stext = ref('我是子組件的值') const sfunction = ()=>{ console.log("我是子組件的方法") } defineExpose({ stext, sfunction }) </script>
父組件:
<template> <div class="todo-container"> <p>我是父組件</p> <son ref="sonDom"></son> <button @click="getSonDom">點(diǎn)擊</button> </div> </template> <script setup> import { ref ,nextTick} from 'vue'; import son from './components/son.vue' const sonDom = ref(null) //注意這里的命名要和ref上面的命名一致 const getSonDom = ()=>{ console.log("sonDom",sonDom.value) } //直接打印sonDom的值是拿不到的,子組件節(jié)點(diǎn)還沒生成 nextTick(()=>{ console.log("sonDom",sonDom.value) }) </script>
到此這篇關(guān)于vue3-setup語法糖之組件傳參(defineProps、defineEmits、defineExpose)的文章就介紹到這了,更多相關(guān)vue3 setup語法糖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3 setup中defineEmits與defineProps的使用案例詳解
- vue3中defineEmits的使用舉例詳解
- 一文詳細(xì)聊聊vue3的defineProps、defineEmits和defineExpose
- Vue3中defineEmits、defineProps?不用引入便直接用
- vue3.0語法糖內(nèi)的defineProps及defineEmits解析
- vue3中組件事件和defineEmits示例代碼
- vue3使用element-plus中el-table組件報(bào)錯(cuò)關(guān)鍵字'emitsOptions'與'insertBefore'分析
- vue3 組合式API defineEmits() 與 emits 組件選項(xiàng)詳解
相關(guān)文章
vue中對(duì)象數(shù)組去重的實(shí)現(xiàn)
這篇文章主要介紹了vue中對(duì)象數(shù)組去重的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Vue JS對(duì)URL網(wǎng)址進(jìn)行編碼解碼,轉(zhuǎn)換為對(duì)象方式
這篇文章主要介紹了Vue JS對(duì)URL網(wǎng)址進(jìn)行編碼解碼,轉(zhuǎn)換為對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue實(shí)現(xiàn)驗(yàn)證碼登錄的方法實(shí)例
最近在自己寫頁面,然后寫登錄注冊UI的時(shí)候需要一個(gè)驗(yàn)證碼組件,去搜一下沒找到什么合適的,于是自己寫一個(gè),這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的相關(guān)資料,需要的朋友可以參考下2022-03-03Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼
在開發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下2024-10-10在vant 中使用cell組件 定義圖標(biāo)該圖片和位置操作
這篇文章主要介紹了在vant 中使用cell組件 定義圖標(biāo)該圖片和位置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳
上傳文件,基本上用了input框就可以解決,但大文件應(yīng)該怎樣上傳呢,一次性上傳明顯不現(xiàn)實(shí),因?yàn)槊看我粩嗑W(wǎng),那就會(huì)從頭開始上傳,所以切片勢在必行,關(guān)鍵就是如何切,怎么保障文件數(shù)據(jù)不會(huì)丟失,同時(shí)優(yōu)化上傳保障機(jī)制,本文就來給大家講講如何上傳大文件2023-07-07Vue開發(fā)中出現(xiàn)Loading?Chunk?Failed的問題解決
本文主要介紹了Vue開發(fā)中出現(xiàn)Loading?Chunk?Failed的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03