Vue3父子组件相互调用方法

文章介绍了在Vue.js中如何使用setup语法糖进行父子组件间的方法调用。子组件需通过defineExpose暴露方法,父组件则通过ref调用子组件方法。反之,子组件可以通过emit触发父组件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面演示均为使用 setup 语法糖的情况!

参考网址:https://cn.vuejs.org/api/sfc-script-setup.html#defineexpose

一、父组件调用子组件方法

子组件需要使用defineExpose对外暴露方法,父组件才可以调用!

1.子组件

<template>
	<div>我是子组件</div>
</template>

<script lang="ts" setup>
	// 第一步:定义子组件的方法
	const hello = (str: string) => {
		console.log('子组件的hello方法执行了--' + str)
	}
	// 第二部:暴露方法
	defineExpose({
		hello
	})
</script>

2.父组件

<template>
	<button @click="getChild">触发子组件方法</button>
	<!-- 一:定义 ref -->
	<Child ref="childRef"></Child>
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import Child from '../../components/child.vue';

	// 二:定义与 ref 同名变量
	const childRef = ref <any> ()

	// 三、函数
	const getChild = () => {
		// 调用子组件的方法或者变量,通过value
		childRef.value.hello("hello world!");
	}
</script>

3.测试结果

二、子组件调用父组件方法

1.父组件

<template>
	<Child @sayHello="handle"></Child>
</template>

<script lang="ts" setup>
	import Child from '../../components/child.vue';

	const handle = () => {
		console.log('子组件调用了父组件的方法')
	}
</script>

2.子组件

<template>
	<view>我是子组件</view>
	<button @click="say">调用父组件的方法</button>
</template>

<script lang="ts" setup>
	const emit = defineEmits(["sayHello"])

	const say = () => {
		emit('sayHello')
	}
</script>

3.测试结果

今天的分享就到这里啦~~

如有错误,欢迎随时雅正。

### Vue3父组件调用子组件方法的实现 在 Vue3 中,父组件可以通过 `ref` 和 `expose` 来调用子组件方法。以下是具体的实现细节: #### 使用 `defineExpose` 暴露子组件方法 为了使父组件能够访问子组件内部定义的方法或数据,子组件需要通过 `defineExpose` 显式暴露这些内容。 ```javascript <script setup> import { ref } from &#39;vue&#39; const counter = ref(0) // 定义一个方法供外部调用 function increment() { counter.value++ } // 将需要暴露的内容传递给 defineExpose defineExpose({ increment, counter }) </script> <template> <div>当前计数: {{ counter }}</div> </template> ``` 在此代码片段中,`increment` 方法和 `counter` 数据被显式暴露[^3]。 --- #### 父组件通过 `ref` 访问子组件实例 父组件可以利用模板语法中的 `ref` 属性绑定到子组件实例,并通过该引用调用已暴露的方法。 ```html <template> <ChildComponent ref="childRef" /> <button @click="callChildMethod">调用子组件方法</button> </template> <script setup> import ChildComponent from &#39;./ChildComponent.vue&#39; import { ref } from &#39;vue&#39; const childRef = ref(null) function callChildMethod() { if (childRef.value && typeof childRef.value.increment === &#39;function&#39;) { childRef.value.increment() } } </script> ``` 在这个例子中,父组件通过 `childRef.value.increment()` 成功调用子组件的 `increment` 方法[^2]。 --- #### 处理多个动态子组件的情况 当存在多个动态生成的子组件时,可以通过 `v-for` 配合自定义逻辑保存每个子组件的引用。 ```html <template> <div> <template v-for="item in list"> <ChildComponent :ref="(el) => setChildRefs(el, item.id)" /> </template> <button @click="updateAllChildren">更新所有子组件</button> </div> </template> <script setup> import ChildComponent from &#39;./ChildComponent.vue&#39; import { reactive } from &#39;vue&#39; const list = reactive([ { id: 1 }, { id: 2 }, { id: 3 } ]) const childRefs = {} function setChildRefs(el, id) { if (el) { childRefs[id] = el } } function updateAllChildren() { Object.values(childRefs).forEach((child) => { if (typeof child.update === &#39;function&#39;) { child.update(&#39;新值&#39;) } }) } </script> ``` 此代码展示了如何管理一组子组件并批量操作它们的方法[^3]。 --- ### 总结 Vue3 提供了一种清晰的方式来让父组件调用子组件方法: 1. **子组件**需使用 `defineExpose` 明确暴露出希望共享的功能。 2. **父组件**则借助 `ref` 获取子组件实例,并通过其属性或方法进行交互。 这种方法不仅增强了父子组件之间的解耦性,还提高了代码可维护性和灵活性。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值