-
父组件:
<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
相关推荐
tedcloud1235 小时前
RTK部署教程:构建稳定的AI Workflow环境ZC跨境爬虫6 小时前
跟着 MDN 学CSS day_16:(深入掌握背景与边框的艺术)愚者Pro8 小时前
Flutter Widget组件学习(专为 Uniapp 转 Flutter 定制)道里9 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验Royzst9 小时前
xml知识点IT_陈寒10 小时前
React useEffect闭包陷阱差点把我整失业了kyriewen10 小时前
推行AI写代码一年后,Code Review变成了新的加班理由前端环境观察室10 小时前
给 Agent Browser Workflow 加一层可观测性:Trace、Snapshot 和 Review Queue柒瑞11 小时前
Superpowers结合Claude code浅实战Nian.Baikal11 小时前
从零搭建离线地图服务:Nginx + Cesium/Leaflet 实战指南