vue3?setup中父組件通過(guò)Ref調(diào)用子組件的方法(實(shí)例代碼)
在setup()鉤子函數(shù)中調(diào)用
父組件
<template> <div> 我是父組件 <children ref="childrenRef" /> <button @click="handleChildren">觸發(fā)子組件</button> </div> </template> <script lang="ts"> import { ref, defineComponent } from 'vue' import Children from './components/Children.vue'; export default defineComponent({ components: { Children } setup() { // ref的泛型除了指定any外 還可以指定<InstanceType<typeof Children>> const childrenRef = ref<any>(); const handleChildren = () => childrenRef.value.isChildren(); return { childrenRef, handleChildren } }, }) </script>
子組件:
<template> <div> 我是子組件 </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ setup() { const isChildren = () => { console.log("我是子組件"); } return { isChildren, } } }) </script>
如果是在setup()
鉤子函數(shù)中使用,父組件通過(guò)ref
獲取到子組件實(shí)例后,直接.value.函數(shù)名
即可調(diào)用子組件所定義的函數(shù)。其中ref的泛型可以指定any
和InstanceType<typeof Children>
在<srcipt setup>中調(diào)用
父組件
<template> <div> 我是子組件 </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ setup() { const isChildren = () => { console.log("我是子組件"); } return { isChildren, } } }) </script>
子組件
<template> <div> 我是子組件 </div> </template> <script setup lang="ts"> import { defineExpose } from 'vue'; const isChildren = () => { console.log("我是子組件的第一個(gè)方法"); } const isChildren2 = () => { console.log("我是子組件的第二個(gè)方法"); } defineExpose({ isChildren, isChildren2 }) </script>
在<srcipt setup>
中調(diào)用和setup()
鉤子函數(shù)中調(diào)用不同的是:子組件中要通過(guò)defineExpose
將方法暴露給父組件。
到此這篇關(guān)于vue3 setup中父組件通過(guò)Ref調(diào)用子組件的方法的文章就介紹到這了,更多相關(guān)vue3 setup父組件調(diào)用子組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)商品分類菜單數(shù)量提示功能
這篇文章主要介紹了Vue實(shí)戰(zhàn)—商品分類菜單數(shù)量提示功能,本文通過(guò)項(xiàng)目實(shí)戰(zhàn)給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07vue3 setup語(yǔ)法糖下的定時(shí)器的使用與銷毀
如果在組件中需要使用定時(shí)器,注意在銷毀組件的時(shí)候,要對(duì)定時(shí)器進(jìn)行銷毀,否則時(shí)間長(zhǎng)了會(huì)導(dǎo)致頁(yè)面卡頓,這篇文章給大家介紹vue3 setup語(yǔ)法糖下的定時(shí)器的使用與銷毀的知識(shí),感興趣的朋友一起看看吧2024-02-02使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果附demo
這篇文章主要介紹了使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果 demo,需要的朋友可以參考下2019-05-05面試官問(wèn)你Vue2的響應(yīng)式原理該如何回答?
可能很多小伙伴之前都了解過(guò)?Vue2實(shí)現(xiàn)響應(yīng)式的核心是利用了ES5的Object.defineProperty?但是面對(duì)面試官時(shí)如果只知道一些模糊的概念。只有深入底層了解響應(yīng)式的原理,才能在關(guān)鍵時(shí)刻對(duì)答如流,本文就來(lái)和大家詳細(xì)聊聊,感興趣的可以收藏一下2022-12-12淺談Vuejs中nextTick()異步更新隊(duì)列源碼解析
本篇文章主要介紹了淺談Vuejs中nextTick()異步更新隊(duì)列源碼解析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Vue實(shí)現(xiàn)簡(jiǎn)單選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例
本篇文章主要介紹了vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Vue 報(bào)錯(cuò)TypeError: this.$set is not a function 的解決方法
這篇文章主要介紹了Vue 報(bào)錯(cuò)TypeError: this.$set is not a function 的解決方法,分享給大家,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12