vue3 语法糖 ref 访问子组件

462 阅读1分钟

官方文档,我们发现官方对其做出了解释说明:使用< script setup>的组件默认是关闭的,即通过模板 ref 或者 $parent 链获取到的组件的公开实例,不会暴露任何在 < script setup> 中声明的绑。

只需要在被调用的子组件中,通过defineExpose暴露对应的函数、属性即可

<script setup>
    const count = ref(0)
    function test() {
        console.log(‘子组件的方法’)
    }

    defineExpose({
        test,
        count
    })
</script>