vue3父組件調(diào)用子組件的方法例子
前言
vue3父組件調(diào)用子組件的方法是通過(guò)expose
和ref
來(lái)實(shí)現(xiàn)的,我們可以通過(guò)expose
來(lái)控制父組件可以訪問(wèn)子組件那些的方法和對(duì)象,我們將通過(guò)setup api
(組合式 api)和option api
(選項(xiàng)式 api)來(lái)演示父組件如何調(diào)用子組件的方法。
1,組合式API
父組件通過(guò)ref定義子組件實(shí)例,通過(guò)調(diào)用實(shí)例獲得子組件的數(shù)據(jù)和方法
<!-- 父組件 app.vue --> <template> <div class="itxst"> <!-- 使用 ref 指令關(guān)聯(lián)子組件 --> <child ref="childComp"/> <button @click="onTry">點(diǎn)擊試一試</button> </div> </template> <script setup> import { reactive, ref } from "vue"; import child from "./child.vue"; //定義子組件實(shí)例,名稱(chēng)要和上面的ref相同 const childComp = ref(null); //訪問(wèn)demo組件的方法或?qū)ο? const onTry = () => { //獲取到子組件的 title 數(shù)據(jù) let msg = childComp.value.state.title; //調(diào)用子組件的 play方法 childComp.value.play(); }; </script>
子組件通過(guò)defineExpose暴露對(duì)象和方法
<!--子組件名稱(chēng) child.vue --> <template> <div class="itxst"> {{ state.title }} </div> </template> <script setup> import { ref, reactive } from "vue"; //定義一個(gè)變量 const state = reactive({ title: "www.itxst.com", }); //定義一個(gè)方法 const play = () => { state.title = "你調(diào)用了子組件的方法"; }; //暴露state和play方法 defineExpose({ state, play, }); </script>
2,選項(xiàng)式API
<!-- 父組件 app.vue --> <template> <div class="itxst"> <!-- 使用 ref 命令 --> <child ref="childComp"/> <button @click="onClick">點(diǎn)擊試一試</button> </div> </template> <script > import child from "./child.vue"; export default { name: "app", //注冊(cè)組件 components: { child, }, methods: { onClick: function () { //獲取到 子組件的 數(shù)據(jù) let msg = this.$refs.childComp.message; //執(zhí)行了子組件的 play方法 this.$refs.childComp.play(); }, }, }; </script>
<!-- 子組件 child.vue --> <template> <div class="itxst"> {{ title }} </div> </template> <script> //選項(xiàng)式默認(rèn)當(dāng)前實(shí)例是全部暴露 export default { name: "demo", //默認(rèn)全部暴露 也可以通過(guò)expose控制那些需要暴露 //expose: ['play'], data() { return { title: "www.itxst.com", }; }, methods: { play: function () { this.title = "你調(diào)用了子組件的方法"; }, }, }; </script>
總結(jié)
到此這篇關(guān)于vue3父組件調(diào)用子組件的文章就介紹到這了,更多相關(guān)vue3父組件調(diào)用子組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中emit('update:modelValue')使用簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于vue3中emit('update:modelValue')使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09vue實(shí)現(xiàn)購(gòu)物車(chē)加減
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車(chē)加減,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05elementui如何解決el-table重復(fù)寫(xiě)loading問(wèn)題
這篇文章主要介紹了elementui如何解決el-table重復(fù)寫(xiě)loading問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue插件實(shí)現(xiàn)過(guò)程中遇到的問(wèn)題總結(jié)
隨著Vue.js越來(lái)越火,Vue.js 的相關(guān)插件也在不斷的被貢獻(xiàn)出來(lái),數(shù)不勝數(shù),這篇文章主要給大家介紹了關(guān)于Vue插件實(shí)現(xiàn)過(guò)程中遇到的問(wèn)題,需要的朋友可以參考下2021-08-08Vue項(xiàng)目中v-bind動(dòng)態(tài)綁定src路徑不成功問(wèn)題及解決
這篇文章主要介紹了Vue項(xiàng)目中v-bind動(dòng)態(tài)綁定src路徑不成功問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue實(shí)踐---vue不依賴(lài)外部資源實(shí)現(xiàn)簡(jiǎn)單多語(yǔ)操作
這篇文章主要介紹了vue實(shí)踐---vue不依賴(lài)外部資源實(shí)現(xiàn)簡(jiǎn)單多語(yǔ)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09為Vue3?組件標(biāo)注?TS?類(lèi)型實(shí)例詳解
這篇文章主要為大家介紹了為Vue3?組件標(biāo)注?TS?類(lèi)型實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08