vue3-setup中如何通过ref调用子组件的函数
子组件通过defineExpose向外导出需要调用的函数
在父子间中定义ref引用来调用
- 子组件关键代码:
<script setup>
import { ref, reactive, defineExpose } from 'vue'
const show = ref(false);
const title = ref('添加收款方式');
const showDialog = (e) => {
title.value = e;
show.value = true;
}
// 导出函数
defineExpose({
showDialog,
})
</script>
- 父组件关键代码:
// vue
<DialogPay ref="dialogPay"
@close="close" />
// js
<script setup>
import { ref } from 'vue';
import DialogPay from '@/views/SpreadManage/DialogPay.vue';
// DialogPay子组件引用
const dialogPay = ref(null);
const withdrawal = () => {
dialogPay.value.showDialog('编辑收款信息')
}
</script>
标签:vue,const,setup,调用,vue3,组件,ref
From: https://www.cnblogs.com/pansidong/p/17999892