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

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

阅读 831
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']) 
}