FormLink在卡片中快速多次点击导致重复跳转或重复调用
harmonyos
我的卡片中有一个FormLink按钮,用户可能会快速连续点击,导致重复跳转或重复调用后台服务。
@Entry
@Component
struct StockCard {
@State stockCode: string = '000001'
@State lastUpdateTime: string = ''
@State isLoading: boolean = false
build() {
Column() {
Text("股票代码: " + this.stockCode)
.fontSize(16)
Text(this.lastUpdateTime ? "更新时间: " + this.lastUpdateTime : "未更新")
.fontSize(12)
.fontColor(Color.Gray)
FormLink({
action: "call",
abilityName: "StockServiceAbility",
bundleName: "com.example.finance",
params: {
'method': 'refreshStockData',
'stockCode': this.stockCode
}
}) {
Button(this.isLoading ? "更新中..." : "刷新数据")
.width(100)
.height(30)
.backgroundColor(this.isLoading ? Color.Gray : '#2196F3')
.enabled(!this.isLoading)
}
.margin(10)
.onClick(() => {
// 尝试在这里处理点击,但FormLink有自己的点击处理
this.isLoading = true
this.lastUpdateTime = '更新中...'
})
}
}
}
问题:
-
用户快速连续点击时,会多次调用StockServiceAbility
-
即使按钮显示为禁用状态,FormLink仍然可以触发
-
没有有效的防抖机制
如何实现FormLink的防重复点击?如何在点击后正确更新卡片状态?