-
父组件:
<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
相关推荐
jump_jump3 小时前
基于 Squoosh WASM 的浏览器端图片转换库小二·6 小时前
前端监控体系完全指南:从错误捕获到用户行为分析(Vue 3 + Sentry + Web Vitals)阿珊和她的猫7 小时前
IIFE:JavaScript 中的立即调用函数表达式阿珊和她的猫8 小时前
`require` 与 `import` 的区别剖析+VX:Fegn08958 小时前
计算机毕业设计|基于springboot + vue在线音乐播放系统(源码+数据库+文档)智商偏低8 小时前
JSEncrypt谎言西西里8 小时前
零基础 Coze + 前端 Vue3 边玩边开发:宠物冰球运动员生成器+VX:Fegn08958 小时前
计算机毕业设计|基于springboot + vue律师咨询系统(源码+数据库+文档)努力的小郑8 小时前
2025年度总结:当我在 Cursor 里敲下 Tab 的那一刻,我知道时代变了GIS之路8 小时前
GDAL 实现数据空间查询