-
父组件:
<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
相关推荐
GISer_Jing1 小时前
React手撕组件和Hooks总结Warren985 小时前
Lua 脚本在 Redis 中的应用mCell6 小时前
JavaScript 运行机制详解:再谈 Event Loopamy_jork8 小时前
npm删除包帧栈10 小时前
开发避坑指南(27):Vue3中高效安全修改列表元素属性的方法max50060010 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现excel10 小时前
使用函数式封装绘制科赫雪花(Koch Snowflake)我命由我1234510 小时前
软件开发 - 避免过多的 if-else 语句(使用策略模式、使用映射表、使用枚举、使用函数式编程)萌萌哒草头将军11 小时前
Node.js v24.6.0 新功能速览 🚀🚀🚀AALoveTouch12 小时前
大麦APP抢票揭秘