欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > defineExpose 显式导出子组件方法

defineExpose 显式导出子组件方法

2025/6/24 18:09:49 来源:https://blog.csdn.net/yingleiming/article/details/142568133  浏览:    关键词:defineExpose 显式导出子组件方法

当父组件调用子组件的script setup中的方法时,必须显式导出该方法。因为 script setup 中定义的变量和方法默认是局部的,只有显式导出后,父组件才能访问这些方法。

//父组件-Parent
<template><el-button type="primary" @click="addOrUpdateHandle()">{{ $i18("add") }}</el-button><!-- 弹窗, 新增 / 修改 --><add-or-update ref="addOrUpdateRef"></add-or-update>
</template>
<script setup>
import { unref } from "vue";
const addOrUpdateHandle = (row) => {const instance = unref(addOrUpdateRef);if (instance && typeof instance.init === "function") {instance.init(row);}
};
</script>
//子组件-Child
<template><el-dialogv-model="visible"></el-dialog>
</template>
<script setup>
import { reactive } from "vue";
const state = reactive({visible: false,
});
const init = () => {state.visible = true;
};
// 使用 defineExpose 导出 init 方法
defineExpose({init,
});
</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词