HarmonyOS this.tabsController.changeIndex(this.currentIndex)不走每一个子组件的onPageShow

Index页面有4个tab,tab详情点击进入到第二个页面,从第二个页面返回到Index页面,怎样刷新每个tab子组建里面的接口?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

tabsController是不走onPageShow的,可以参考以示例如下:

// index.ets:
import { SecondPage } from './SecondPage'
import { ThirdPage } from './ThirdPage'

@Entry
@Component
struct TabsExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  @State mainPageState: boolean = false
  @Builder
  tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
        TabContent() {
          SecondPage({ mainPageState: this.mainPageState, currentIndex: this.currentIndex })
            .width('100%')
            .height('100%')
            .backgroundColor('#00CB87')

        }.tabBar(this.tabBuilder(0, 'green'))

        TabContent() {
          ThirdPage({ mainPageState: this.mainPageState, currentIndex: this.currentIndex })
            .width('100%')
            .height('100%')
            .backgroundColor('#00CB87')
        }.tabBar(this.tabBuilder(1, 'blue'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.tabBuilder(2, 'yellow'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.tabBuilder(3, 'pink'))
      }
      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width('100%')
      .height('100%')
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
  }

  onPageShow(): void {
    console.log('index--- onPageShow')
    this.mainPageState = true
  }

  onPageHide(): void {
    console.log('index--- onPageHide')
    this.mainPageState = false
  }
}
// SecondPage.ets:
import { router } from '@kit.ArkUI';
import { FaultLogger } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
export struct SecondPage {
  @State message: string = 'Hello World';
  @Link @Watch('mainIsShow') mainPageState: boolean
  @Link @Watch('mainIsShow') currentIndex: number

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {
          router.pushUrl({
            url: 'pages/DetailPage',

          })

        })
      }
      .width('100%')
    }
    .height('100%')
  }

  mainIsShow() {
    if (this.currentIndex == 0) {
      console.log('green显示了--', this.mainPageState)
    }

  }

  aboutToAppear(): void {
    console.log('firstPAGE--- aboutToAppear')
  }

  onPageShow(): void {
    console.log('firstPAGE--- onPageShow')
  }

  onPageHide(): void {
    console.log('firstPAGE--- onPageHide')
  }

  aboutToRecycle(): void {
    console.log('firstPAGE--- aboutToRecycle')
  }
}
// ThirdPage.ets
import { router } from '@kit.ArkUI';
import { FaultLogger } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
export struct ThirdPage {
  @State message: string = 'Hello World';
  @Link @Watch('mainIsShow') mainPageState: boolean
  @Link @Watch('mainIsShow') currentIndex: number

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {
          router.pushUrl({
            url: 'pages/DetailPage',

          })

        })
      }
      .width('100%')
    }
    .height('100%')
  }

  mainIsShow() {
    if (this.currentIndex == 1) {
      console.log('blue显示了--', this.mainPageState)
    }
  }

  aboutToAppear(): void {
    console.log('firstPAGE--- aboutToAppear')
  }

  onPageShow(): void {
    console.log('firstPAGE--- onPageShow')
  }

  onPageHide(): void {
    console.log('firstPAGE--- onPageHide')
  }

  aboutToRecycle(): void {
    console.log('firstPAGE--- aboutToRecycle')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
销毁一个子窗口方法
419浏览 • 1回复 待解决
HarmonyOS navigation触发onpageshow
14浏览 • 1回复 待解决