-
父组件:
<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
相关推荐
belldeep2 分钟前
python:用 Flask 3 , mistune 2 和 mermaid.min.js 10.9 来实现 Markdown 中 mermaid 图表的渲染凉辰22 分钟前
使用uni.createInnerAudioContext()播放指定音频(踩坑分享功能)echoVic37 分钟前
多模型支持的架构设计:如何集成 10+ AI 模型程序员Agions40 分钟前
useMemo、useCallback、React.memo,可能真的要删了echoVic40 分钟前
AI Agent 安全权限设计:blade-code 的 5 种权限模式与三级控制David凉宸42 分钟前
Vue 3 + TS + Vite + Pinia vs Vue 2 + JS + Webpack + Vuex:对比分析滕青山43 分钟前
Vue项目BMI计算器技术实现子兮曰1 小时前
深入浏览器指纹:Canvas、WebGL、Audio是如何暴露你的身份的?月亮补丁1 小时前
AntiGravity只能生成 1:1 图片?一招破解尺寸限制何中应1 小时前
MindMap部署