vue3下,父组件调用子组件的方法,如果使用了<script setup> 这种写法,那么子组件方法需要采用defineExpose()进行修饰,才能被外界调用。上代码:

1、子组件

_pop.vue:

<template>
	。。。
</template>

<script setup>
	import { defineExpose } from "vue";
	
	const popIt = () => {
		。。。
	};
	defineExpose({ popIt });
</script>

2、父组件


<template>
	<pop pTitle="hehe" ref="pop1"></pop>
</template>

<script setup>
	import pop from "./_pop";

	const pop1 = ref();
	pop1.value.popIt();
</script>

参考文章;
Vue 3 父子组件互调方法 - setup 语法糖写法

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐