-
父组件:
<script setup> import { defineAsyncComponent, watchEffect, toRefs, reactive } from 'vue';// 异步组件
const Test = defineAsyncComponent(()=>import('./xx/Test.vue'))const child1Ref = ref(null)
</script> <template> {{ state.age }} -- {{ state.name }} <button @click="state.sayHello">say hello</button> <Test ref="child1Ref"/> </template>
const state = reactive({
age: 1,
name: '2',
sayHello: null,
})
watchEffect(() => {
// 拿到子组件的一些数据
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 setup> import { ref, defineExpose } from 'vue'const a = ref(101)
const b = ref('sddewfewfew')const onSayHello = () => {
</script> <template>
console.log('hello')
}
defineExpose({
a,
b,
onSayHello,
})Child1
</template>
vue3通过ref获取子组件defineExpose的数据和方法
qq_427506082023-10-08 8:58
相关推荐
spencer_tseng14 分钟前
jquery download极速蜗牛37 分钟前
告别部署焦虑!PinMe:前端开发者的极简部署神器uhakadotcom1 小时前
Python Protobuf 全面教程:常用 API 串联与实战指南by__csdn1 小时前
微前端架构:从理论到实践的全面解析漫长的~以后2 小时前
Edge TPU LiteRT V2拆解:1GB内存设备也能流畅跑AI的底层逻辑小福气_2 小时前
自定义组件 vue3+elementPlus程序员博博2 小时前
这才是vibe coding正确的打开方式 - 手把手教你开发一个MCP服务piaoroumi2 小时前
UVC调试前端不太难2 小时前
RN 调试效率低,一点小改动就需要重新构建?解决手册(实战 / 脚本 / Demo)是谁眉眼2 小时前
vue环境变量