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)
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
HarmonyOS
2025-01-09 16:42:56
792浏览
收藏 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')
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
分享
微博
QQ
微信
回复
2025-01-09 19:27:35
相关问题
HarmonyOS 首页组件生命周期问题
731浏览 • 1回复 待解决
HarmonyOS 关于组件重绘的生命周期函数
1059浏览 • 1回复 待解决
如何使用AbilityStage的生命周期函数
3527浏览 • 1回复 待解决
HarmonyOS页面onPageShow生命周期不回调
1842浏览 • 1回复 待解决
HarmonyOS TabContent页面生命周期触发
1330浏览 • 1回复 待解决
HarmonyOS 生命周期触发
698浏览 • 1回复 待解决