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

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

HarmonyOS
2024-05-22 22:53:44
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
退休的程序员

参考以下写法,参数类无法传递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; 
    }) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-23 17:14:15
相关问题
HarmonyOS router跳转返回如何
707浏览 • 1回复 待解决
HarmonyOS Navigation问题
1351浏览 • 1回复 待解决
HarmonyOS 页面导航跳转回调问题
2335浏览 • 0回复 待解决
HarmonyOS 页面如何
1771浏览 • 1回复 待解决
JS跳转Java问题?
5804浏览 • 1回复 待解决
HarmonyOS 页面问题
1290浏览 • 1回复 待解决
HarmonyOS 路由跳转获取跳转的方式
2033浏览 • 1回复 待解决
HarmonyOS 调用flutter页面
650浏览 • 1回复 待解决
HarmonyOS 页面后对象出错
587浏览 • 1回复 待解决
路由如何?可否给个案例?
4114浏览 • 1回复 待解决
HarmonyOS Web组件加载本地页面问题
1285浏览 • 1回复 待解决
HarmonyOS navigation页面之间回
843浏览 • 1回复 待解决