项目场景:

例如:项目场景:父组件调用子组件里表格清空多选的方法


问题描述

报错: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>

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐