-
父组件:
<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
相关推荐
木童6621 分钟前
Docker 容器化 Web 服务全流程实践(含网络深度场景)长安牧笛4 分钟前
制作本地美食测评评分工具,输入美食名称,口味,价格,自动生成评分,帮助消费者选择美食。Aftery的博客4 分钟前
Uniapp-vue实现语言功能切换(多语言)一字白首5 分钟前
Vuex 进阶,模块化开发(Modules):解决单状态树臃肿问题喵了几个咪6 分钟前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:用 JavaScript/Lua 解锁动态业务扩展能力旧梦吟12 分钟前
脚本网页 linux内核源码讲解李少兄1 小时前
深入理解 CSS :not() 否定伪类选择器程序员码歌8 小时前
短思考第261天,浪费时间的十个低效行为,看看你中了几个?Swift社区8 小时前
React Navigation 生命周期完整心智模型