vue3父组件通过ref调用子组件的方法
vue3父组件通过ref调用子组件的方法
·
Index.vue
<script setup>
import { ref, onMounted } from 'vue'
import Child from './Child.vue'
import './index.css'
const child = ref(null)
onMounted(() => {
child.value.handleGetValue()
})
</script>
<template>
<div class="m-home-wrap">
<Child ref="child"></Child>
<div class="m-home-demo"></div>
</div>
</template>
<style></style>
Child.vue(选项式):
<template>
<div>child</div>
</template>
<script>
export default {
methods: {
handleGetValue() {
console.log('子组件的方法')
}
}
}
</script>
<style>
</style>
Child.vue(组合式):
<template>
<div>child</div>
</template>
<script lang="ts" setup>
import { defineExpose } from 'vue'
const handleGetValue = () => {
console.log('子组件的方法')
}
defineExpose({
handleGetValue
})
</script>
<style></style>

人工智能学习网站
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)