HarmonyOS 在使用router.pushUrl\(\{url: url, params: params\}\)进行页面间传递数据时无法接收map类型参数?

如题:HarmonyOS 在使用router.pushUrl({url: url, params: params})进行页面间传递数据时无法接收map类型参数?

阅读 624
1 个回答

解决方案:router不支持map类型的数据传参,params中只能包含基础类型的数据.可以使用Record代替map

private routerParams : RouterParams = new RouterParams()
private param : Record<string, string> = {};
routePage() {
  try {
    this.param['vip_source'] = 'qww'
    this.routerParams.pageType = '11'
    this.routerParams.params = this.param
    this.routerParams.title = '新闻'
    router.pushUrl({
      url: 'pages/RouterA',
      params: this.routerParams
    })
  } catch (err) {
    console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
  }
}

@State value:Record<string, Record<string,string>> = router.getParams() as Record<string, Record<string,string>>;

onPageShow() {
  console.log("hh2:" + this.value['title'])
  console.log("hh3:" + this.value['params']['vip_source'])
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进