HarmonyOS 首页进入组件页面后执行this.pageInfos.pop()返回首页后不执行onPageShow生命周期函数

build() {
  Navigation(this.pageInfos) {
    Scroll() {
      Column() {
        //初始化地图
        // this.buildMapShow()
        Button('API自动化导航组件', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'NaviCompositeApi' })
            // router.pushUrl({
            //   url: 'pages/composite/NaviTestIndex'
            // })
          })

        Button('导航组件', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'NaviComposite' })
          })

        Button('导航组件(货车)', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'TruckNaviComposite' })
          })

        Button('公共组件', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'CommonComposite' })
          })

        Button('语音播报', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'TextToSpeech' })
          })

        Button('导航管理-开始导航', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'DriveNaviNoMap' })
          })

        Button('显示地图', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'ShowMapTest' })
          })

        Button('定位设置', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'DemoGlobalSetting' })
          })

        Button('模拟定位', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'MockLocation' })
          })
      }
    }
  }
  .title('导航Demo')
  .navDestination(this.PageMap)
  .mode(NavigationMode.Stack)
  .titleMode(NavigationTitleMode.Full)
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

参考以下代码,首页3s后自动跳转,跳转后点击返回首页,回调onShown方法,3s后再次自动跳转:

@Entry
@Component
struct Index {
  private sysNavStack: NavPathStack = new NavPathStack();

  aboutToAppear(): void {
    this.sysNavStack.pushPathByName("NavDestination1", null)
  }

  @Builder
  PageMap(url: string) {
    if (url === 'NavDestination1') {
      NavDestination1()
    } else if (url === 'NavDestination2') {
      NavDestination2()
    }
  }

  build() {
    Navigation(this.sysNavStack)
      .hideTitleBar(true)
      .navDestination(this.PageMap)
      .hideNavBar(true)
      .width('100%')
      .height('100%')
      .backgroundColor(Color.Red)
  }
}

@Component
export struct NavDestination1 {
  private sysNavStack?: NavPathStack;

  build() {
    NavDestination() {
      Text("我是首页").width(200).height(100).fontColor(Color.Blue).fontSize(26)
    }
    .width('100%')
    .height('100%')
    .mode(NavDestinationMode.STANDARD)
    .onReady((context) => {
      this.sysNavStack = context.pathStack
    })
    .onShown(() => {
      console.log('cwq NavDestination1 onShown')
      setTimeout(() => {
        this.sysNavStack?.pushPathByName("NavDestination2", null, true)
      }, 3000)
    })
    .onHidden(() => {
      console.log('cwq NavDestination1 onHidden')
    })
  }
}

@Component
export struct NavDestination2 {
  private sysNavStack?: NavPathStack;

  build() {
    NavDestination() {
      Row() {
        Column() {
          Text('NavDestnation2 page')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              this.sysNavStack?.pop();
            })
        }
        .width('100%')
      }
      .height('100%')
    }
    .width('100%')
    .height('100%')
    .mode(NavDestinationMode.STANDARD)
    .onReady((context) => {
      this.sysNavStack = context.pathStack;
    })
    .onShown(() => {
      console.log('cwq NavDestination2 onShown')
    })
    .onHidden(() => {
      console.log('cwq NavDestination2 onHidden')
    })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 首页组件生命周期问题
31浏览 • 1回复 待解决
如何使用AbilityStage的生命周期函数
2641浏览 • 1回复 待解决
HarmonyOS页面onPageShow生命周期不回调
1162浏览 • 1回复 待解决
HarmonyOS TabContent页面生命周期触发
590浏览 • 1回复 待解决
HarmonyOS 生命周期触发
190浏览 • 1回复 待解决