vue3 父子组件之间的方法调用

16 篇文章 0 订阅
写法1

父组件通过为子组件绑定ref属性调用子组件的方法

  1. 父组件的内容
<template>
  <div class="container">
    <childComponent ref="childRef"></childComponent>
    <el-button type="primary" size="default" @click="touchEvent">点击触发子组件方法</el-button>
  </div>
</template>

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

export default {
  components: {
    childComponent
  },

  setup() {
    const childRef = ref(null)

    const touchEvent = () => {
      childRef.value.firstEvent()
    }

    return {
      childRef,
      touchEvent
    }
  }
}
</script>
  1. 子组件的内容
<template>
  <div class="container">
    <div>
      <span>父组件触发了{{ count }}次子组件的方法</span>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {

    const count = ref(0)

    const firstEvent = () => {
      count.value++
    }

    return {
      count,
      firstEvent
    }
  }
}
</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eOEed4gH-1658307849413)(C:\Users\zll\AppData\Roaming\Typora\typora-user-images\1658306031165.png)]

父组件通过自定义事件,子组件通过emit调用父组件的方法

  1. 父组件的内容
<template>
  <div class="container">
    <span>子组件调用了{{ count }}次父组件的方法</span>
    <childComponent @parentEvent="touchEvent"></childComponent>
  </div>
</template>

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

export default {
  components: {
    childComponent
  },
  setup() {
    const count = ref(0)

    const touchEvent = (data) => {
      count.value++
      console.log(data) // 接受子组件传递过来的值
    }

    return {
      count,
      touchEvent
    }
  }
}
</script>
  1. 子组件的内容
<template>
  <div class="container">
    <el-button type="primary" size="default" @click="parentEvent">点击触发父组件方法</el-button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup(props, ctx) {
    console.log(props, ctx);
    const parentEvent = () => {
      ctx.emit('parentEvent');
      ctx.emit('parentEvent', '向父组件传递的值') //可以向父组件传值
    }

    return {
      parentEvent
    }
  }
}
</script>

在这里插入图片描述

写法2

使用 <script setup> 的组件是默认关闭的,即通过模板 ref 或者 $parent 获取到组件的公开实例,不会暴露任何在 <script setup> 中声明的绑定。 为了在 <script setup> 组件中明确要暴露出去的属性,使用 defineExpose 编译器宏, 同样必须使用 definePropsdefineEmits API来声明propsemit

父组件通过为子组件绑定ref属性调用子组件的方法
2.1 父组件的内容

<template>
  <div class="container">
    <childComponent ref="childRef"></childComponent>
    <el-button type="primary" size="default" @click="touchEvent">点击触发子组件方法</el-button>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import childComponent from '../components/childComponent.vue'

const childRef = ref(null)
const touchEvent = () => {
  childRef.value.firstEvent()
}

</script>

2.2 子组件的内容

  • 子组件的方法使用defineExpose暴露出来
<template>
  <div class="container">
    <div>
      <span>父组件触发了{{ count }}次子组件的方法</span>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";

const count = ref(0)
const firstEvent = () => {
  count.value++
}

defineExpose({ firstEvent })
</script>

子组件通过emit调用父组件的方法

2.1 父组件的内容

<template>
  <div class="container">
    <span>子组件调用了{{ count }}次父组件的方法</span>
    <childComponent @parentEvent="touchEvent"></childComponent>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import childComponent from '../components/childComponent.vue'

const count = ref(0)
const touchEvent = (data) => {
  console.log(data) //data接收子组件传过来的值 user: {...}
  count.value++
}
</script>

2.2 子组件的内容

  • 子组件的方法使用defineEmits暴露出来
<template>
  <div class="container">
    <el-button type="primary" size="default" @click="parentEvent">点击触发父组件方法</el-button>
  </div>
</template>

<script setup>
import { onMounted } from "vue";
const user = {
	name: '小明',
	age: 25,
}
const emit = defineEmits([ "parentEvent" ]);
const parentEvent = () => {
  // 传递user到父组件
  emit('parentEvent', user);
}
</script>

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,组件可以通过props属性向组件传递数据,而组件可以通过$emit方法组件发送信息。下面是一个简单的示例来说明父子组件之间如何调用方法: 在组件中,首先需要在组件上定义一个ref属性,用于获取组件实例的引用。然后,在需要调用组件方法的地方,可以通过该引用来调用组件方法。 ```vue <template> <div> <ChildComponent ref="child"></ChildComponent> <button @click="callChildMethod">调用组件方法</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { callChildMethod() { this.$refs.child.childMethod(); // 调用组件方法 }, }, }; </script> ``` 在组件中,需要将需要调用方法定义在methods选项中,并通过$emit方法组件发送消息。 ```vue <template> <div> <button @click="childMethod">组件方法</button> </div> </template> <script> export default { methods: { childMethod() { this.$emit('childMethodCalled'); // 向组件发送消息 }, }, }; </script> ``` 在组件中,可以通过监听组件的事件来接收消息。 ```vue <template> <div> <ChildComponent @childMethodCalled="handleChildMethodCalled"></ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { handleChildMethodCalled() { // 处理组件方法调用 }, }, }; </script> ``` 通过以上方式,组件就可以调用组件方法了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值