Vue3父组件触发子组件方法

在Vue3中,可以通过ref来获取子组件实例,然后调用子组件的方法。具体步骤如下:

  1. 在子组件中,使用ref来获取子组件实例:

import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  setup() {
    const childComponentRef = ref(null)

    const triggerChildMethod = () => {
      childComponentRef.value.childMethod()
    }

    return {
      childComponentRef,
      triggerChildMethod
    }
  }
}

<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent',
  data() {
    return {
      message: 'Hello from child component!'
    }
  },
  methods: {
    childMethod() {
      console.log('Child method called!')
    }
  }
}
</script>

2.在父组件中,使用ref来获取子组件实例,并定义一个方法来触发子组件的方法:

<template>
  <div>
    <ChildComponent ref="childComponentRef" />
    <button @click="triggerChildMethod">Trigger child method</button>
  </div>
</template>

<script>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  setup() {
    const childComponentRef = ref(null)

    const triggerChildMethod = () => {
      childComponentRef.value.childMethod()
    }

    return {
      childComponentRef,
      triggerChildMethod
    }
  }
}
</script>

3。在父组件中,使用ref来获取子组件实例,并在setup函数中定义一个方法来触发子组件的方法。然后在模板中绑定一个按钮,当点击按钮时,调用triggerChildMethod方法来触发子组件的方法:

<template>
  <div>
    <ChildComponent ref="childComponentRef" />
    <button @click="triggerChildMethod">Trigger child method</button>
  </div>
</template>

<script>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  setup() {
    const childComponentRef = ref(null)

    const triggerChildMethod = () => {
      childComponentRef.value.childMethod()
    }

    return {
      childComponentRef,
      triggerChildMethod
    }
  }
}
</script>

或者子组件用 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>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,组件可以通过使用`ref`和`expose`来触发组件方法。首先,组件需要使用`ref`来获取组件的实例,并定义一个方法触发组件方法。然后,在组件的模板中,可以使用`@click`来绑定一个事件,该事件会调用刚刚定义的方法,从而触发组件方法。 下面是一个示例代码,演示了如何在Vue3中触发组件方法: ```html <template> <div> <ChildComponent ref="childComponentRef" /> <button @click="triggerChildMethod">Trigger child method</button> </div> </template> <script> import { ref } from 'vue' import ChildComponent from './ChildComponent.vue' export default { name: 'ParentComponent', components: { ChildComponent }, setup() { const childComponentRef = ref(null) const triggerChildMethod = () => { childComponentRef.value.childMethod() } return { childComponentRef, triggerChildMethod } } } </script> ``` 在上述代码中,组件通过`ref`定义了组件的实例,并且定义了一个`triggerChildMethod`方法触发组件方法。在模板中,当点击按钮时,`triggerChildMethod`方法会被调用,从而触发组件方法。 请注意,这只是一个示例,实际代码可能会有所不同,具体取决于你的组件结构和需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue3组件调用组件方法](https://blog.csdn.net/weixin_42252416/article/details/128001894)[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* [Vue3组件触发组件方法](https://blog.csdn.net/wangwengrui40/article/details/130699748)[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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值