-
父组件:
<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
相关推荐
用户5757303346242 小时前
🐱 从“猫厂”倒闭到“鸭子”横行:一篇让你笑出腹肌的 JS 面向对象指南Moment2 小时前
腾讯终于对个人开放了,5 分钟在 QQ 里养一只「真能干活」的 AI 😍😍😍码路飞2 小时前
GPT-5.4 Computer Use 实战:3 步让 AI 操控浏览器帮你干活 🖥️比尔盖茨的大脑2 小时前
AI Agent 架构设计:从 ReAct 到 Multi-Agent 系统天才熊猫君2 小时前
使用 Vite Mode 实现客户端与管理端的物理隔离HelloReader2 小时前
React Hook 到底是干嘛的?用户60572374873082 小时前
OpenSpec 实战:从需求到代码的完整工作流进击的尘埃2 小时前
Service Worker 离线缓存这事,没你想的那么简单进击的尘埃2 小时前
HTTP/3 的多路复用和 QUIC 到底能让页面快多少?聊聊连接迁移和 0-RTT