el-tabs中如果用 v-for动态增加tab,每个tab中如果需要控制自己控件的增删改查,就需要设置动态ref.
1. 先定义一个空字典:
const inputRefMap = ref({});
2. 然后定义一个方法将变量增加到字典中:
const handleSetInputMap = (el: refItem, item: number) => {
if (el) {
inputRefMap.value[`Input_Ref_${item}`] = el;
}
};
3. template中调用该方法:
:ref="(el:refItem) => handleSetInputMap(el, item.Id)"
4. 使用该变量:
const addClick = (item: number) => {
inputRefMap.value[`Input_Ref_${item}`].addRow();
};
全部代码参考如下:
<script setup lang="ts">
import { ComponentPublicInstance } from "vue";
const inputRefMap = ref({});
const listInfo = ref<Array<any>>();
type refItem = Element | ComponentPublicInstance | null;
const handleSetInputMap = (el: refItem, item: number) => {
if (el) {
inputRefMap.value[`Input_Ref_${item}`] = el;
}
};
const addClick = (item: number) => {
// 调用该控件下的方法 .addRow()
inputRefMap.value[`Input_Ref_${item}`].addRow();
};
</script>
<template>
<el-tabs type="border-card">
<el-tab-pane
v-for="(item, index) in listInfo"
:key="index"
:label="item.EquipName"
>
<PartDivider title="竞对中标结果更新" v-if="item.Competitors.length">
<template #handle>
<el-button
type="primary"
size="small"
v-if="editStatus"
@click="addClick(item.Id)"
link
>新增</el-button
>
</template>
<bidCompetitorResultList
:ref="(el:refItem) => handleSetInputMap(el, item.Id)"
:FormSn="item.FormSn"
:competitorList="item.Competitors"
:BidResultsList="BidResults"
:editStatus="editStatus"
:currencyType="CurrencyShort(item.Currency)"
/>
</PartDivider>
</el-tab-pane>
</el-tabs>
</template>