在主页面定义一个变量
@State refreshHomeKey: number = 0;
在切换TabContent时候更改refreshHomeKey 值
Column() {
Tabs({ barPosition: BarPosition.End, index: this.currentIndex }) {
TabContent() {
VehicleAssistant({ refreshKey: this.refreshHomeKey })
}.tabBar(this.TabBuilder(0, '首页'))
}
.width('100%')
.height('100%')
.barMode(BarMode.Fixed)
.onChange((index: number) => {
console.log("Tab changed to: " + index)
this.currentIndex = index
// Tab切换时的刷新逻辑
if (index === 0) {
console.log("111111")
this.refreshHomeKey ++
}
})
}
在tab页面监听refreshHomeKey 值的变化
@Prop @Watch('onRefreshKeyChanged') refreshKey: number = 0;
// 监听 refreshKey 变化
onRefreshKeyChanged() {
this.loadCarInfo();
}