HarmonyOS router.getParams()数据传递

router.getParams()数据接收异常

A - B

B - A 返回数据 B发送数据,A接收。

A - B

B - A B不发送数据,A会接收上一次传递的数据。

A页面:

@Entry 
@Component 
struct AddDeviceTypePage { 
  private readonly TAG:string = 'AddDeviceTypePage' 
  @State routerParams: Record<string, string> | null = null 
  private scanCodeResult:string = '' 
  onPageShow() { 
    this.routerParams = router.getParams() as Record<string, string> 
    if (this.routerParams !== undefined && this.routerParams !== null) { 
      this.scanCodeResult = this.routerParams.scanResult 
    } 
    ToastUtils.showToast(this.scanCodeResult + '') 
  } 
}

B页面(第一次调用这个方法,第二次不调用,也会触发router.getParams()接收上一次的数据):

router.back({ 
  url: 'pages/add_device/AddDeviceTypePage', 
  params: { 
    scanResult: result[0].originalValue 
  }})
HarmonyOS
2024-09-02 09:36:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

A页面判断的时候只判断了 this.routerParams 存在的时候,在第二次返回页面的时候 this.routerParams 为 undefined ,this.scanCodeResult保留的还是上次的赋值,可以在后面加个else判断。示例如下:

onPageShow() { 
  this.routerParams = router.getParams() as Record<string, string> 
  if (this.routerParams !== undefined && this.routerParams !== null) { 
    this.scanCodeResult = this.routerParams.scanResult 
  } else { 
    this.scanCodeResult = '' 
  } 
  console.log(this.scanCodeResult + '') 
}
分享
微博
QQ
微信
回复
2024-09-02 17:47:28
相关问题
router.getParams使用问题
2341浏览 • 1回复 待解决
HarmonyOS router.getParams()清空问题
1331浏览 • 1回复 待解决
HarmonyOS router.getParams无法获取,报错
1278浏览 • 1回复 待解决
HarmonyOS 数据传递问题
1456浏览 • 1回复 待解决
HarmonyOS worker间数据传递
1145浏览 • 1回复 待解决
HarmonyOS 关于Provide数据传递问题咨询
1236浏览 • 1回复 待解决
HarmonyOS axios上传文件数据传递问题
1052浏览 • 1回复 待解决
多hap调用及数据传递,有人知道吗?
1974浏览 • 1回复 待解决
router传递hashmap参数问题
2860浏览 • 1回复 待解决
Router传递Object对象解读
2501浏览 • 1回复 待解决