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
相关问题
HarmonyOS 数据传递问题
177浏览 • 1回复 待解决
HarmonyOS 关于Provide数据传递问题咨询
139浏览 • 1回复 待解决
多hap调用及数据传递,有人知道吗?
669浏览 • 1回复 待解决
HarmonyOS taskpool数据传输问题
126浏览 • 1回复 待解决
HarmonyOS 视频流数据传
141浏览 • 1回复 待解决
Router传递Object对象解读
990浏览 • 1回复 待解决
router传递hashmap参数问题
1328浏览 • 1回复 待解决
返回页面router.back如何传递参数
338浏览 • 2回复 待解决
如何获取router.back传递的参数
2156浏览 • 1回复 待解决
手机如何与电脑端进行数据传
2925浏览 • 1回复 待解决
页面关闭时如何传递数据
1727浏览 • 1回复 待解决
HarmonyOS router跳转问题
130浏览 • 2回复 待解决
如何在onPageShow中区分getParams来源
1728浏览 • 1回复 待解决