vue3之 defineExpose() 函数,子组件暴露接口给父组件直接调用

什么是 defineExpose()?

defineExpose() 是 Vue 3 提供的一个 Composition API 函数,用于明确指定哪些内部响应式状态或函数可以被外部访问。

如何使用 defineExpose()?

在子组件中定义:

const  submit= () => {
  const result = A({
    接口传参数据
  });
  const res = B({
    接口传参数据
  });
  return Promise.all([result, res]).then((values) => {
    const bol = values.every((item) => {
      !item.success && msg.error(item.message);
      return item.success;
    });
    return bol;
  });
};


defineExpose({
  submit,
});

注意:defineExpose() 应该放在末尾,因为任何在它之后声明的变量或函数都不会被自动包含在暴露的对象中。 

 在父组件中访问:

<modal>
    <组件名 ref="syncRef" />
    <template #footer>
      <n-button-primary @click="submit">确定</n-button-primary>
    </template>
</modal>
import 组件名 from './components/组件名文件位置.vue';

const syncRef = ref<InstanceType<typeof 组件名> | null>(null);

const handleSubmitSync = async () => {
  const result = await syncRef.value?.submit();
  result;
};

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,要实现父组件调用组件函数,可以使用`ref`和`defineExpose`。首先,在组件中定义需要返回给父组件的方法,并使用`defineExpose`将方法传递给父组件。例如,在组件的`setup`函数中,定义了一个名为`colorMapValChildFn`的函数,我们可以使用`defineExpose`将其传递给父组件组件代码如下所示(引用): ```javascript import { reactive, toRefs, ref, onMounted, defineExpose } from "vue"; export default { name: "colorModal", setup(props, { emit }) { const colorMapValChildFn = () => { // 组件函数逻辑 // ... }; // 需要返回给父组件的方法,使用defineExpose传递出去 defineExpose({ colorMapValChildFn }); return { // 当然,在setup里也要return需要暴露给父组件函数 colorMapValChildFn }; } }; ``` 接下来,在父组件中,我们可以通过在组件中使用`ref`来获取组件的实例,然后就可以调用组件函数了。需要注意的是,调用组件函数需要在父组件中等待组件加载完成,可以使用`onMounted`生命周期钩函数来确保组件加载完成后再进行调用。以下是父组件调用组件函数的示例代码(引用和): ```javascript <template> <div> <color-modal ref="childComponent" /> <button @click="callChildFn">调用组件函数</button> </div> </template> <script> import ColorModal from "@/components/ColorModal.vue"; export default { components: { ColorModal }, methods: { callChildFn() { if (this.$refs.childComponent) { this.$refs.childComponent.colorMapValChildFn(); } } } }; </script> ``` 在父组件中,我们使用了`ref`属性来引用组件,并在`callChildFn`方法中通过`this.$refs.childComponent`来获取组件的实例,在实例上调用组件的`colorMapValChildFn`方法。 请注意,在父组件调用组件函数之前,确保组件已经加载完毕,可以在父组件的`mounted`钩函数调用组件函数,或者使用`onMounted`钩函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue组件调用组件函数的方法](https://download.csdn.net/download/weixin_38659159/12941063)[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_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue3父组件调用组件里的方法](https://blog.csdn.net/ydZ157/article/details/126179621)[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_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值