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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue前端開發(fā)之實(shí)現(xiàn)交錯(cuò)過渡動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了如何通過Vue實(shí)現(xiàn)交錯(cuò)過渡動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟
本次小編給大家?guī)淼氖顷P(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-02-02
詳解element-ui動(dòng)態(tài)限定的日期范圍選擇器代碼片段
這篇文章主要介紹了element-ui動(dòng)態(tài)限定的日期范圍選擇器代碼片段,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
解決VUE項(xiàng)目使用Element-ui 下拉組件的驗(yàn)證失效問題
這篇文章主要介紹了解決VUE項(xiàng)目使用Element-ui 下拉組件的驗(yàn)證失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue點(diǎn)擊頁面空白處實(shí)現(xiàn)保存功能
這篇文章主要介紹了vue點(diǎn)擊頁面空白處實(shí)現(xiàn)保存功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
vue-resourse將json數(shù)據(jù)輸出實(shí)例
這篇文章主要為大家詳細(xì)介紹了vue-resourse將json數(shù)據(jù)輸出實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

