如何在navigation跳转页面时返回传参

如何在navigation跳转页面时返回传参

HarmonyOS
2024-05-22 22:53:44
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
li_jian_jun

参考以下写法,参数类无法传递map等js对象。

@Entry 
@Component 
struct Index { 
  @Provide('pathInfos') pathInfos: NavPathStack = new NavPathStack(); 
 
  @Builder 
  myRouter(name: string) { 
    if (name === 'MyFirstNavDestination') { 
      MyFirstNavDestination() 
    } else if (name === 'MySecondNavDestination') { 
      MySecondNavDestination() 
    } 
  } 
 
  build() { 
    Navigation(this.pathInfos) { 
      Row() { 
        Column() { 
          Text('hello world') 
        } 
        .height('100%') 
      } 
      .onClick(() => { 
        this.pathInfos.pushPathByName('MyFirstNavDestination', { paramA: 'test1' }); 
      }) 
    } 
    .navDestination(this.myRouter) 
  } 
} 
 
@Component 
export struct MyFirstNavDestination { 
  @Consume('pathInfos') pathInfos: NavPathStack; 
 
  getParamsPrint() { 
    console.info('xuerui', 'param is ' + JSON.stringify(this.pathInfos.getParamByName('MyFirstNavDestination'))); 
  } 
 
  build() { 
    NavDestination() { 
      Row() { 
        Column() { 
          Text('MyFirstNavDestination') 
        } 
        .width('100%') 
      } 
      .height('100%') 
      .onClick(() => { 
        this.pathInfos.pushPathByName('MySecondNavDestination', null); 
      }) 
    }.onShown(() => { 
      this.getParamsPrint(); 
    }) 
  } 
} 
 
@Component 
export struct MySecondNavDestination { 
  @Consume('pathInfos') pathInfos: NavPathStack; 
  private routerParams: string = 'test 2'; 
 
  build() { 
    NavDestination() { 
      Row() { 
        Text('MySecondNavDestination') 
      } 
      .height('100%') 
    }.onBackPressed(() => { 
      // pop B页面 
      this.pathInfos.pop(); 
 
      //获取当前栈顶页面名字(A页面) 
      let allPathName: Array<string> = this.pathInfos.getAllPathName(); 
      let pathNameA: string = allPathName[allPathName.length - 1]; 
 
      // pop A页面 
      this.pathInfos.pop(); 
 
      // 重新PUSH A页面 
      this.pathInfos.pushPath(new NavPathInfo(pathNameA, { paramA: this.routerParams })) 
      return true; 
    }) 
  } 
}
分享
微博
QQ
微信
回复
2024-05-23 17:14:15
相关问题
JS跳转Java问题?
3394浏览 • 1回复 待解决
路由如何?可否给个案例?
1832浏览 • 1回复 待解决
tp5 如何对post进行加密?
736浏览 • 1回复 待解决
返回页面不触发aboutToAppear
1278浏览 • 1回复 待解决
跨设备启动FA问题
6685浏览 • 1回复 待解决
弹窗跳转页面返回弹窗不消失
437浏览 • 1回复 待解决
Js FA拉起Java FA时候怎么
3391浏览 • 1回复 待解决
烧录问题,我有一点疑惑
6102浏览 • 4回复 待解决
求教一个sql语句里怎么写?
2103浏览 • 1回复 待解决