-
父组件:
<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
相关推荐
落魄江湖行2 分钟前
入门篇六 Nuxt4错误处理:给应用装个安全气囊薛定猫AI6 分钟前
【技术干货】用 design.md 驯服 AI 生成前端:从 Awesome Design 到工程化落地实践kyriewen9 分钟前
你的JS代码总在半夜崩溃?TypeScript来“上保险”了iReachers27 分钟前
HTML打包EXE配置管理教程:多项目打包设置一键保存、加载与切换武藤一雄30 分钟前
WPF中ViewModel之间的5种通讯方式霍理迪39 分钟前
Vue路由——routewhuhewei42 分钟前
js事件循环TheRouter43 分钟前
构建一个支持多模型的 AI 聊天应用:React + TheRouter API 全栈教程xiaofan11061 小时前
Pretext:无 DOM 的多行文本测量与排版库yuki_uix1 小时前
面试题里的 Custom Hook 思维:从三道题总结「异步状态管理」通用模式