环境:HBuilder X 4.23.2024070309-alpha iOS系统。
我的业务场景:
通过for循环显示多个自定义组件,通过 :ref 动态的给每个组件赋值标识,点击某个按钮时,通过$ref获取组件实例,然后通过实例调用自定义组件方法。
遇到问题:for循环 通过:ref 动态的给每个组件赋值标识,然后通过$ref获取自定义组件 调用组件方法,会出现调用报错。但是如果通过 ref 写死标识,不会报错。
代码段1:
<view v-for="(item,index) in paramList" :key="index" class="chart-container-view">
<modelChartView :ref="paramList[index].refName" :paramModel="paramList[index]"></modelChartView>
</view>
js代码:
buttonEvent() {
const child = this.$refs['chart0'] as ComponentPublicInstance
child.$callMethod('updateXYAxis')
}
代码段2:
<modelChartView ref="chart0" :paramModel="paramList[0]"></modelChartView>
<modelChartView ref="chart1" :paramModel="paramList[1]"></modelChartView>
<modelChartView ref="chart2" :paramModel="paramList[2]"></modelChartView>
<modelChartView ref="chart3" :paramModel="paramList[3]"></modelChartView>
js代码:
buttonEvent() {
const child = this.$refs['chart0'] as ComponentPublicInstance
child.$callMethod('updateXYAxis')
}
代码段3:
<modelChartView :ref="paramList[0].refName" :paramModel="paramList[0]"></modelChartView>
<modelChartView :ref="paramList[1].refName" :paramModel="paramList[1]"></modelChartView>
<modelChartView :ref="paramList[2].refName" :paramModel="paramList[2]"></modelChartView>
js代码:
buttonEvent() {
const child = this.$refs['chart0'] as ComponentPublicInstance
child.$callMethod('updateXYAxis')
}
上述两段代码,代码段1 有for循环,调用方法报错,代码段2、代码段3 是正常的,请问问题出在哪里?还是uniapp-x框架有bug?
报错:
error: child.$callMethod is not a function. (In 'child.$callMethod('updateXYAxis')', 'child.$callMethod' is undefined)