HarmonyOS 关于router.back()

1.第一个页面打开 第二个页面,第二页面打开第三个页面,  第三个页面确定后 回退到第一个页面,router.back()无法使用命名方式跳转会第一个页面。

2.有多个页面都实现了跳转一个页面,有的需要返回结果给跳转的页面,怎么实现功能达到回退结果给跳转的页面

这些功能页面都在har中,无法使用路由方式,只能使用命名方式

HarmonyOS
2024-08-29 11:27:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

router不能满足该场景推荐使用Navigation组件来处理页面跳转 文档链接:

​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5 ​

数据传递可以在pop的时候在onPop回调中,参考demo

//xxx.MainPage4

import {MainPage5} from '../mainpage5/MainPage5' 
import {MainPage6} from '../mainpage6/MainPage6' 
@Entry({ routeName: 'MainPage4' }) 
@Component 
export struct MainPage4 { 
 @State message: string = 'MainPage4'; 
 @Provide('pageStack') pageStack: NavPathStack = new NavPathStack(); 
 @State data: string = '还没收到' 
 @StorageLink('data') data2: string = ''; 
 
 @Builder pageMap(name: string) { 
  if (name ==='MainPage5'){ 
   MainPage5() 
  }else if (name === 'MainPage6') { 
   MainPage6() 
  } 
 } 
 build() { 
  Navigation(this.pageStack){ 
   Column() { 
    Text('返回的数据:'+this.data) 
    Text('返回的数据2:'+this.data2) 
    Text(this.message+'  TOMainPage5') 
     .fontSize(15) 
     .fontWeight(FontWeight.Bold) 
     .onClick(() => { 
      let pathInfo : NavPathInfo = new NavPathInfo('MainPage5', "4传递给5的数据", (popInfo: PopInfo) => { 
       //返回的数据 
       this.data = popInfo.result as string 
      }) 
      this.pageStack.pushDestination(pathInfo, true); 
     }) 
 
    Text(this.message+'  TOMainPage6') 
     .fontSize(15) 
     .fontWeight(FontWeight.Bold) 
     .onClick(() => { 
      let pathInfo : NavPathInfo = new NavPathInfo('MainPage6', "4传递给6的数据", (popInfo: PopInfo) => { 
       this.data = popInfo.result as string 
      }) 
      this.pageStack.pushDestination(pathInfo, true); 
     }) 
   } 
   .width('100%') 
 
  } 
  .navDestination(this.pageMap) 
  .title("根页面MainPage4") 
 } 
}
  • 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.

//xxx.MainPage5

@Component 
export struct MainPage5 { 
 @State message: string = 'MainPage5'; 
 @Consume('pageStack') pageStack: NavPathStack; 
 @State data: string = '' 
 
 build() { 
  NavDestination() { 
   Text('MainPage5') 
   Text('传递的数据:'+this.data) 
   Button('5到6', { stateEffect: true, type: ButtonType.Capsule }) 
    .width('80%') 
    .height(40) 
    .margin(20) 
    .onClick(()=>{ 
     let pathInfo : NavPathInfo = new NavPathInfo('MainPage6', "5传递给6的数据", (popInfo: PopInfo) => { 
      this.data = popInfo.result as string 
     }) 
     this.pageStack.pushPath(pathInfo,true) 
    }) 
  } 
  .onBackPressed(()=>{ 
   const popDestinationInfo = this.pageStack.pop("5返回上页的数据",true) 
   console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo)) 
   return true 
  }) 
  .onReady(( cxt )=>{ 
   console.log('②当NavDestination即将构建子组件之前会触发此回调') 
   //获取页面传递来的数据=>方式1 
   //this.data = cxt.pathInfo.param as string 
   //方式2 
   this.data = this.pageStack.getParamByIndex(this.pageStack.getAllPathName().length-1) as string 
  }) 
 } 
}
  • 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.

//xxx.MainPage6

@Component 
export struct MainPage6 { 
 @State message: string = 'MainPage6'; 
 @Consume('pageStack') pageStack: NavPathStack; 
 @State data: string = '' 
 
 build() { 
  NavDestination() { 
   Text('MainPage6') 
   Text('传递的数据:'+this.data) 
   Button('6到4', { stateEffect: true, type: ButtonType.Capsule }) 
    .width('80%') 
    .height(40) 
    .margin(20) 
    .onClick(()=>{ 
     console.log('6-4') 
     //清除栈回到首页 
     this.pageStack.clear() 
     AppStorage.setOrCreate("data","data") 
    }) 
  } 
  .onBackPressed(()=>{ 
   const popDestinationInfo = this.pageStack.pop("6返回上页的数据",true) 
   console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo)) 
   return true 
  }) 
  .onReady(( cxt )=>{ 
   console.log('②当NavDestination即将构建子组件之前会触发此回调') 
   //获取页面传递来的数据=>方式1 
   //this.data = cxt.pathInfo.param as string 
   //方式2 
   this.data = this.pageStack.getParamByIndex(this.pageStack.getAllPathName().length-1) as string 
  }) 
 } 
}
  • 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.

数据的存取也可以通过应用全局的UI状态存储AppStorage实现

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-appstorage-V5

分享
微博
QQ
微信
回复
2024-08-29 17:11:01
相关问题
HarmonyOS 关于router.back问题的咨询
1175浏览 • 1回复 待解决
HarmonyOS router.back不起作用
663浏览 • 1回复 待解决
HarmonyOS router.back返回页面问题
806浏览 • 1回复 待解决
HarmonyOS router.back无法退出应用
766浏览 • 1回复 待解决
HarmonyOS Vue页面router.back()无法返回
723浏览 • 1回复 待解决
HarmonyOS router.back返回参数如何接收
687浏览 • 1回复 待解决
HarmonyOS router.back()如何获取返回值
1199浏览 • 1回复 待解决
如何获取router.back传递的参数
3579浏览 • 1回复 待解决
返回页面router.back如何传递参数
1613浏览 • 2回复 待解决
HarmonyOS router.back无法返回到上一页
628浏览 • 1回复 待解决
router.back调用时,偶现app自动退出
2322浏览 • 1回复 待解决
能否拦截左滑返回并替换为router.back
1217浏览 • 1回复 待解决
router.back中加入指定url后无法返回
924浏览 • 1回复 待解决
router.back如何携带参数返回给上一级
2396浏览 • 1回复 待解决