-
父组件:
<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
相关推荐
空中海1 天前
第七章:vue工程化与构建工具zhensherlock1 天前
Protocol Launcher 系列:Trello 看板管理的协议自动化zhuà!1 天前
element的el-form提交校验没反应问题龙猫里的小梅啊1 天前
CSS(一)CSS基础语法与样式引入小码哥_常1 天前
从0到1,开启Android音视频开发之旅渔舟小调1 天前
P19 | 前端加密通信层 pikachuNetwork.js 完整实现qq_12084093711 天前
Three.js 工程向:Draw Call 预算治理与渲染批处理实践旷世奇才李先生1 天前
Vue3\+Vite\+Pinia实战:企业级后台管理系统完整实现(附源码)不会聊天真君6471 天前
JavaScript基础语法(Web前端开发笔记第三期)