-
父组件:
<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
相关推荐
jump6803 分钟前
url输入到网页展示会发生什么?诸葛韩信6 分钟前
我们需要了解的Web Workersbrzhang12 分钟前
我觉得可以试试 TOON —— 一个为 LLM 而生的极致压缩数据格式yivifu31 分钟前
JavaScript Selection API详解这儿有一堆花33 分钟前
告别 Class 组件:拥抱 React Hooks 带来的函数式新范式十二春秋1 小时前
场景模拟:基础路由配置六月的可乐1 小时前
实战干货-Vue实现AI聊天助手全流程解析一 乐1 小时前
智慧党建|党务学习|基于SprinBoot+vue的智慧党建学习平台(源码+数据库+文档)BBB努力学习程序设计2 小时前
CSS Sprite技术:用“雪碧图”提升网站性能的魔法Zyx20072 小时前
深拷贝:JavaScript 中对象复制的终极解法