-
父组件:
<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
相关推荐
南山安几秒前
从反转字符串看透面试官的“内心戏”:你的算法思维到底怎么样?东华帝君几秒前
vue3自定义v-modelwww_stdio几秒前
用 localStorage 打造本地待办清单:一个轻量级的前端实践JIngJaneIL3 分钟前
远程在线诊疗|在线诊疗|基于java和小程序的在线诊疗系统小程序设计与实现(源码+数据库+文档)fruge20 分钟前
搭建个人博客 / 简历网站:从设计到部署的全流程(含响应式适配)光影少年25 分钟前
css影响性能及优化方案都有哪些呆呆敲代码的小Y1 小时前
2025年多家海外代理IP实战案例横向测评,挑选适合自己的q***3751 小时前
爬虫学习 01 Web Scraper的使用v***5651 小时前
Spring Cloud Gatewayb***59431 小时前
分布式WEB应用中会话管理的变迁之路