defineExpose的使用(父组件获取子组件的实例)

官方解释:

使用 <script setup> 的组件是默认关闭的——即通过模板引用或者 $parent 链获取到的组件的公开实例,不会暴露任何在 <script setup> 中声明的绑定。

可以通过 defineExpose 编译器宏来显式指定在 <script setup> 组件中要暴露出去的属性:

当父组件通过模板引用的方式获取到当前组件的实例,获取到的实例会像这样 { a: number, b: number } (ref 会和在普通实例中一样被自动解包)

通俗的讲:父组件可以通过defineExpose 获取子组件的实例

应用场景:当子组件的按钮都在父组件,需要统一在父组件点击按钮后操作就可以使用这种方法(个人使用场景推荐);

例子:

//子组件——————defineExpose出组件中的a变量和loadList 函数,父组件就可以直接通过实例使用
<script setup lang="ts">
import { ref } from 'vue'

const a = ref(2)
// loadList 函数
const loadList = (item: any) => {
  console.log(item)
}

defineExpose({
  a,
  loadList
})
</script>
<template>
  <div>child</div>
</template>
//父组件————在引入的子组件身上定义ref="childeRef"
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import ChildComponent from '@/views/articles/ChildComponent.vue'
const childeRef = ref()
onMounted(() => {
  childeRef.value.loadList([{ id: 1, name: '22' }]) // 调用子组件函数
})
</script>
<template>
  <ChildComponent ref="childeRef"></ChildComponent>
</template>

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue 3中,可以使用defineExpose来暴露组件的方法。在组件中,需要先通过ref来获取组件实例,然后才能调用组件的方法。首先,在组件setup选项中,使用defineExpose来定义需要暴露的方法。例如,在组件Comp.vue中,可以使用defineExpose({ show })来暴露show方法。然后,在组件使用ref来获取组件实例,并通过调用实例的方法来调用组件的show方法。具体代码如下所示: ```javascript // 组件Comp.vue <script setup> import { ref, defineExpose } from 'vue' const show = () => { alert("我是组件,我的show方法被调用了") } // 主动暴露childMethod方法 defineExpose({ show }) </script> <template> <div>我是组件</div> </template> // 组件app.vue <script setup> import { ref } from 'vue' import Comp from './Comp.vue' const showComp = ref(null) // 这个时候获取组件Comp const childShow = () => { showComp.value.show() // 调用组件的show方法 } </script> <template> <button @click='childShow'>点击调用组件方法</button> <Comp ref='showComp'></Comp> </template> ``` 以上是一个示例代码,可以在组件中通过点击按钮调用组件的show方法。在组件setup选项中,使用defineExpose来暴露show方法。然后在组件使用ref来获取组件实例,并通过调用实例的方法来调用组件的show方法。这样就实现了在Vue 3中使用defineExpose来暴露组件的方法。 <span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [vue3组件如何调用组件的方法?需要defineExpose方法主动暴露方法](https://blog.csdn.net/weixin_41791737/article/details/123280412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue 组件中调用组件函数的方法](https://download.csdn.net/download/weixin_38659159/12941063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值