-
父组件:
<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
相关推荐
孤水寒月2 小时前
基于HTML的悬窗可拖动记事本祝余呀2 小时前
html初学者第一天脑袋大大的3 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘速易达网络4 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案耶啵奶膘5 小时前
uniapp+firstUI——上传视频组件fui-upload-videoJoJo_Way5 小时前
LeetCode三数之和-js题解视频砖家5 小时前
移动端Html5播放器按钮变小的问题解决方法lyj1689976 小时前
vue-i18n+vscode+vue 多语言使用小白变怪兽7 小时前
一、react18+项目初始化(vite)ai小鬼头7 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图