在vue2中想要调用子组件里的方法直接使用this.$ref.OrderListRef.getList()这种方式即可,在vue3中有几个点需要注意:
1. 子组件需要暴露对外公开的变量(defineExpose)
<script lang="ts" setup>
import { defineComponent, reactive, toRefs,onMounted,getCurrentInstance, ref, watch } from 'vue';
const getList = (listSearchInfo)=> {
}
defineExpose({
getList
})
</script>
2. 父组件使用:
<template>
<OrderList ref="OrderListRef"></OrderList>
</template>
<script lang="ts" setup>
import { defineComponent, reactive, toRefs,onMounted,getCurrentInstance, ref, watch } from 'vue';
import OrderList from "./components/OrderList.vue";
const OrderListRef = ref(); // 获取
onMounted(()=>{
OrderListRef.value.getList() //使用
})
</script>