-
父组件:
<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
相关推荐
thatway19893 小时前
闲聊-关于AI终结者的警醒努力的小郑3 小时前
突发!Claude Code 51万行源码全网裸奔:一场史诗级“开源”事故,国内大厂笑麻了七度黑光3 小时前
用 openclaw 给故障复盘打分:质量审核自动化实践HashTang3 小时前
Claude Code 源码中 REPL.tsx 深度解析:一个 5005 行 React 组件的架构启示wendycwb4 小时前
前端城市地址根据最后一级倒推,获取各层级id的方法终端鹿4 小时前
Vue3 模板引用 (ref):操作 DOM 与子组件实例 从入门到精通千寻girling5 小时前
不知道 Java 全栈 + AI 编程有没有搞头 ?小码哥_常5 小时前
Android开发:精准捕获应用的前后台行踪蜡台5 小时前
Vue 打包优化木斯佳5 小时前
前端八股文面经大全:快手前端一面 (2026-03-29)·面经深度解析