vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法
更新時間:2023年10月09日 09:29:34 作者:qq_42750608
defineExpose是Vue3中新增的選項,用于向父組件暴露子組件內(nèi)部的屬性和方法,通過defineExpose,子組件可以主動控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下
1. 父組件:
<script setup> import { defineAsyncComponent, watchEffect, toRefs, reactive } from 'vue'; // 異步組件 const Test = defineAsyncComponent(()=>import('./xx/Test.vue')) const child1Ref = ref(null) const state = reactive({ age: 1, name: '2', sayHello: null, }) watchEffect(() => { // 拿到子組件的一些數(shù)據(jù) console.log(child1Ref.value) const obj = toRefs(child1Ref.value) console.log(obj.a, obj.b) state.name = obj.b state.age = obj.a state.sayHello = obj.onSayHello }) </script> <template> {{ state.age }} -- {{ state.name }} <button @click="state.sayHello">say hello</button> <Test ref="child1Ref"/> </template>
2. 子組件
<script setup> import { ref, defineExpose } from 'vue' const a = ref(101) const b = ref('sddewfewfew') const onSayHello = () => { console.log('hello') } defineExpose({ a, b, onSayHello, }) </script> <template> <p>Child1</p> </template>
到此這篇關(guān)于vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法的文章就介紹到這了,更多相關(guān)vue3獲取defineExpose內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element中table組件(el-table)右側(cè)滾動條空白占位處理
當(dāng)我設(shè)置了max-height,就會在表格右側(cè)出現(xiàn)一列空白的占位,本文主要介紹了Element中table組件(el-table)右側(cè)滾動條空白占位處理,感興趣的可以了解一下2023-09-09淺談vue中數(shù)據(jù)雙向綁定的實現(xiàn)原理
本篇文章主要介紹了淺談vue中數(shù)據(jù)雙向綁定的實現(xiàn)原理 ,主要使用v-model這個數(shù)據(jù)雙向綁定,有興趣的可以了解一下2017-09-09vue+element-ui+sortable.js實現(xiàn)表格拖拽功能
這篇文章主要為大家詳細(xì)介紹了vue+element-ui+sortable.js實現(xiàn)表格拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04vite(vue3)配置內(nèi)網(wǎng)ip訪問的方法步驟
Vite是一個快速的構(gòu)建工具,Vue3是一個流行的JavaScript框架,下面這篇文章主要給大家介紹了關(guān)于vite(vue3)配置內(nèi)網(wǎng)ip訪問的方法步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05