-
父组件:
<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
相关推荐
Web极客码2 小时前
WordPress 6.8有哪些新特性UXbot7 小时前
UI设计工具推荐合集码路星河7 小时前
基于 Vue + VueUse 的 WebSocket 优雅封装:打造高可用的全局连接管理方案摘星编程7 小时前
React Native + OpenHarmony:Accelerometer计步器代码敲敲了个代码7 小时前
如何优化批量图片上传?队列机制+分片处理+断点续传三连击!(附源码)@AfeiyuO8 小时前
Vue 引入全局样式scss光影少年8 小时前
flex布局和grid布局区别,实现两边固定布局中间自适应全栈测试笔记8 小时前
异步函数与异步生成器EndingCoder8 小时前
配置 tsconfig.json:高级选项木风小助理8 小时前
JavaStreamAPI的性能审视,优雅语法背后的隐形成本与优化实践