Vue3父组件访问子组件数据 defineExpose用法

在Vue3中,父组件可通过创建一个ref(null),然后将赋值的元素写在当前子组件上即可,在需要的时候,通过定义的响应式变量即可获取,获取后即可取得当前子组件内部dom以及当前子组件内部变量方法等,并且直接使用子组件内部方法。但是有时候获取的时候返回的没有什么信息只有一个{_v_skin:true}这个信息,这条信息表示数据无法响应。

父组件示例

<template>
  <div class="container">
      <h-demo ref="childDom" />

      <button @click="getChild"></button>
  </div>
</template>

<script setup>
import Hdemo from '@/components/Hdemo';
import { ref, } from 'vue';
const childDom=ref(null)


onMounted(() => {
  const getChild = () =>{
    console.log(childDom.value)
    // 打印当前子组件暴露出来的数据
    childDom.value.handleVal()
    // 执行子组件方法
  }
})
</script>
<style lang="scss" scoped>

</style>

子组件示例


<script setup>
import { ref } from 'vue';
    const demo=ref('测试用例')
    const  handleVal=()=>{
        demo.value="已改变"
    }
</script>
<template>
  <div class="inner">
      {{demo}}
      <button @click="handleVal">改值</button>
  </div>
</template>
<style lang="scss" scoped>

</style>

此时childDom.value的值为{_v_skin:true}无法获取子组件内部信息

重点

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

方法:为了在 <script setup> 语法糖组件中明确要暴露出去的属性,,使用 defineExpose 编译器宏将需要暴露出去的变量与方法放入暴露出去就可以

子组件使用defineExpose 暴露示例



<script setup>
import { ref } from 'vue';
    const demo=ref('测试用例')
    const  handleVal=()=>{
        demo.value="已改变"
    }
//将需要暴露出去的数据与方法都可以暴露出去
    defineExpose({
        demo,
        handleVal,
    })
</script>
<template>
  <div class="inner">
      {{demo}}
      <button @click="handleVal">改值</button>
  </div>
</template>
<style lang="scss" scoped>

</style>

此时父组件即可获取子组件内部变量与方法与当前dom等信息,父组件必须在onMounted钩子函数中才能访问到ref中的值。

附上vue3官方文档说明

 

  • 11
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
如果组件引用了组件但没有在模板中创建组件的标签,你无法直接调用组件中通过`defineExpose`暴露的方法。这是因为在Vue 3中,`defineExpose`只能被组件模板中的组件访问。 如果你想在组件中调用组件的方法,一种可行的方式是使用`ref`来获取组件实例,并通过`.value`来访问组件的方法。以下是一个示例: 在组件中,使用`ref`来创建对组件实例的引用: ```vue <template> <div> <!-- 不创建组件标签 --> <button @click="callChildMethod">调用组件方法</button> </div> </template> <script> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; export default { setup() { const childComponentRef = ref(null); const callChildMethod = () => { // 检查组件实例是否存在 if (childComponentRef.value) { childComponentRef.value.childMethod(); } }; return { callChildMethod, childComponentRef }; }, components: { ChildComponent } }; </script> ``` 在组件中,通过`defineExpose`暴露需要调用的方法: ```vue <template> <!-- 组件模板内容 --> </template> <script> import { defineComponent, defineExpose } from 'vue'; export default defineComponent({ setup() { const childMethod = () => { // 组件方法逻辑 }; defineExpose({ childMethod }); // 其他组件逻辑 return {}; } }); </script> ``` 在组件中,通过 `childComponentRef.value.childMethod()` 来调用组件中的 `childMethod` 方法。确保在调用组件方法之前,检查组件实例是否存在。这样即使没有创建组件标签,也可以调用组件中通过`defineExpose`暴露的方法。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值