在TabContent中進(jìn)行了頁面 currentIndex 的下標(biāo)判斷,這樣做會讓整個頁面重新渲染??梢园秧撁嫦聵?biāo)傳遞給子組件,在子組件中使用@Watch裝飾器對接收的父組件下標(biāo)進(jìn)行裝飾,達(dá)到狀態(tài)變量更改通知。具體使用如下
父組件中的tabs
Tabs({ index: this.currentIndex }) {
TabContent() {
QualityNumView({ homeTabIndex: this.currentIndex })
}
TabContent() {
TodayAlarmView({ homeTabIndex: this.currentIndex })
}
TabContent() {
DeviceLocationView({ homeTabIndex: this.currentIndex })
}
TabContent() {
GasTypeView({ homeTabIndex: this.currentIndex })
}
}
.animationDuration(0)
.layoutWeight(1)
.height(AppConstants.FULL_PERCENT)
.width(AppConstants.FULL_PERCENT)
.barHeight(0)
.scrollable(false)
.onChange((index) => {
this.currentIndex = index;
})
子組件中如下: 這樣做每次切換就只會加載onRefresh方法請求數(shù)據(jù) ,首次加載頁面進(jìn)入方法aboutToAppear
@Component
export struct GasTypeView {
//使用@Watch裝飾器 狀態(tài)變量更改通知 如果父組件的下標(biāo)更改了
//那就會接收到通知進(jìn)入onRefresh方法 在onRefresh中進(jìn)行數(shù)據(jù)請求
@Link @Watch('onRefresh') homeTabIndex: number
//后邊進(jìn)行tab頁面切換就會進(jìn)入此方法
onRefresh() {
if (this.homeTabIndex === 1) {
this.getData()
}
}
//子組件中aboutToAppear方法只會加載一次
aboutToAppear(): void {
if (this.homeTabIndex === 1) {
this.getData()
}
}
getData() {
// 進(jìn)行數(shù)據(jù)請求
}
build() {
}
}