-
父组件:
<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
相关推荐
new code Boy10 分钟前
NestJS、Nuxt.js 和 Next.jsHighcharts.js25 分钟前
Highcharts 使用指南Treegraph chart 树状图/结构树图|创建谱系图表、决策树、结构知识树等的图表工具进击切图仔26 分钟前
执行 shell 脚本 5 种方式对比局i27 分钟前
React 简单地图组件封装:基于高德地图 API 的实践(附源码)执行部之龙38 分钟前
AI对话平台核心技术解析进击的尘埃39 分钟前
Service Worker + stale-while-revalidate:让页面"假装"秒开的正经方案yuki_uix41 分钟前
防抖(Debounce):从用户体验到手写实现HelloReader42 分钟前
Flutter 进阶 UI搭建 iOS 风格通讯录应用(十一)wjj不想说话43 分钟前
在 Vue 2.6 微前端架构中,我们为什么放弃了 Vuex 管理页面状态?张元清43 分钟前
每个 React 开发者都需要的 10 个浏览器 API Hooks