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") 
 } 
}

//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 
  }) 
 } 
}

//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 
  }) 
 } 
}

数据的存取也可以通过应用全局的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问题的咨询
134浏览 • 1回复 待解决
返回页面router.back如何传递参数
338浏览 • 2回复 待解决
如何获取router.back传递的参数
2156浏览 • 1回复 待解决
router.back调用时,偶现app自动退出
1472浏览 • 1回复 待解决
能否拦截左滑返回并替换为router.back
453浏览 • 1回复 待解决
router.back如何携带参数返回给上一级
803浏览 • 1回复 待解决
关于router和Navigation要选择哪个使用
193浏览 • 2回复 待解决
HarmonyOS router跳转问题
128浏览 • 2回复 待解决
HarmonyOS router无法跳转,并报错。
144浏览 • 1回复 待解决
HarmonyOS router传参获取问题
136浏览 • 1回复 待解决
HarmonyOS router路由路径的问题
183浏览 • 1回复 待解决
HarmonyOS router.pushUrl跳转报100002
111浏览 • 1回复 待解决
HarmonyOS router.getParams()数据传递
109浏览 • 1回复 待解决
HarmonyOS router打开har下页面的问题
120浏览 • 1回复 待解决
Class Router和namespace router有什么区别
1396浏览 • 1回复 待解决