项目场景:
例如:项目场景:父组件调用子组件里表格清空多选的方法
问题描述
报错:TypeError: xxxx is not a function
父组件
<public-table ref="publicTableRef"/>
<script setup>
const publicTableRef = ref()
const getSonClear = () => {
publicTableRef.value.getClearSelection()
}
</script>
子组件
<el-table
:data="tableData"
:border="border"
style="width: 100%"
row-key="id"
max-height="620"
ref="tableRef"
lazy
@selection-change="handleSelection"
/>
<script setup>
const tableRef = ref()
// 清空多选方法
const getClearSelection = () => {
tableRef.value.clearSelection()
}
</script>
解决方案:
需要在子组件里用defineExpose导出方法,列如:
子组件
<el-table
:data="tableData"
:border="border"
style="width: 100%"
row-key="id"
max-height="620"
ref="tableRef"
lazy
@selection-change="handleSelection"
/>
<script setup>
const tableRef = ref()
// 清空多选方法
const getClearSelection = () => {
tableRef.value.clearSelection()
}
defineExpose({
getClearSelection
})
</script>