-
父组件:
<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
相关推荐
张拭心1 小时前
编程最强的模型,竟然变成了国产的它爱勇宝1 小时前
2026一人公司生存指南:用AI大模型,90天跑出你的第一条现金流fe小陈1 小时前
简单高效的状态管理方案:Hox + ahooks我叫黑大帅1 小时前
Vue3和Uniapp的爱恨情仇:小白也能懂的跨端秘籍Panzer_Jack1 小时前
如何用 WebGL 去实现一个选取色彩背景图片透明化小工具 - Pick AlphaNone3211 小时前
【NestJs】使用Winston+ELK分布式链路追踪日志采集GIS之路1 小时前
ArcGIS Pro 中的 Python 入门树獭非懒1 小时前
告别繁琐多端开发:DivKit 带你玩转 Server-Driven UI!兆子龙2 小时前
当「多应用共享组件」成了刚需:我们从需求到模块联邦的落地小史Qinana2 小时前
从代码到智能体:MCP 协议如何重塑 AI Agent 的边界