-
父组件:
<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
相关推荐
IT_陈寒3 分钟前
折腾一天才明白:Vite的热更新为什么偶尔会罢工AI茶水间管理员1 小时前
学习ClaudeCode源码之Agent核心循环挖稀泥的工人1 小时前
AI聊天界面的布局细节和打字跟随方法竹林8181 小时前
从“连接失败”到丝滑登录:我用 ethers.js 连接 MetaMask 的完整踩坑记录颜酱1 小时前
图片大模型实践:可灵(Kling)文生图前后端实现Reart1 小时前
从0解构tinyWeb项目--(Day:2)木斯佳1 小时前
前端八股文面经大全:腾讯CSIG实习面(2026-04-10)·面经深度解析孜孜不倦不忘初心1 小时前
Vue 项目结构与命名规范夏暖冬凉2 小时前
npm发布流程(记录遇到的问题)XPoet2 小时前
AI 编程工程化:Subagent——给你的 AI 员工打造协作助手