HarmonyOS Navigation根页面没有onWillShow和onWillHidden方法

Navigation 根页面没有 onWillShow 和 onWillHidden 方法,从子页面返回后,需要执行刷新操作,没有对应的生命周期方法, onPageShow 和 onPageHide 也不会执行, 有什么解决方案

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

关于Navigation 的 onWillShow 和 onWillHidden 方法可以参考文档,里面有示例代码:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-container-tabcontent.md#onwillhide12

也可以尝试使用onVisibleAreaChange回调函数,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-component-visible-area-change-event-V5,下面是一个demo案例:

@Entry
@Component
struct TabBarStyleExample1 {
  @State message:string = "hello word"
  @State color:Color = Color.Black
  @Builder
  MyColum(){
    Childs({colors:this.color,message:this.message})
  }
  build() {
    Column({ space: 5 }) {
      Text("底部页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Pink'))
          .onWillShow(() => {
            this.message = "Pink"
            this.color = Color.Pink
            console.info("Pink will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Pink will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
          .onWillShow(() => {
            this.message = "Yellow"
            this.color = Color.Yellow
            console.info("Yellow will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Yellow will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
          .onWillShow(() => {
            this.message = "Blue"
            this.color = Color.Blue
            console.info("Blue will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Blue will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
          .onWillShow(() => {
            this.message = "Green"
            this.color = Color.Green
            console.info("Green will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Green will hide")
          })
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)

    }
  }
}

@Reusable
@Component
struct Childs{
  @State colors:Color = Color.Green;
  @Prop@Watch('onChage') message:string = ''

  onChage(){
    console.log(':::onChage')
  }

  build() {
    Column(){
      Text(this.message)
    }.width('100%').height('100%').backgroundColor(this.colors)
    .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
      console.info(':::Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
    })

  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS Navigationrouter如何选择
4浏览 • 1回复 待解决
HarmonyOS Navigation动态跳转页面问题
33浏览 • 1回复 待解决
HarmonyOS navigation页面之间回传值
18浏览 • 1回复 待解决