HarmonyOS 鸿蒙Next中HMRouter传参问题

HarmonyOS 鸿蒙Next中HMRouter传参问题

HMRouter传参可以A->B,但是不能A->B->C,可能还有c->A,代码

A:界面代码

Refresh({ refreshing: $$this.refreshing, builder: this.refreshBuilder() }){
List({ space: 3}) {
  ForEach((this.toBeSent), (item: toBeSentData,index:number) => {
    ListItem() {
      Column() {
        Row() {
          Text(item.title)
            .fontColor('#000000')
            .fontSize(20)
          Text(this.getStateInfo(item.status).text)
            .fontSize(20)
            .fontColor(this.getStateInfo(item.status).color)
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        .padding({left:14,right:14,bottom:11,top:10})
        .height(29)
        Row() {
          Image($r('[base].media.time'))
            .width(25)
            .aspectRatio(1)
          Text(item.upDateTime)
            .fontColor('#595959')
            .fontSize(13)
        }
        .width('100%')
        .justifyContent(FlexAlign.Start)
        .padding({left:16,right:16,top:11})

      }.width('100%')
      .height(80)
      .backgroundColor(this.getStateInfo(item.status).backgroundColor)
      .borderRadius(8)
      .onClick(()=>{
        HMRouterMgr.push({pageUrl:PAGE_PATH.to_be_sent_edit,param:item})
      })
    }

    .swipeAction({
      end: {
        builder: () => {
          this.itemEnd(index)
        },
      },
    })

  })
  ListItem() {
    this.footer();
  }
  ListItem(){
    Column().width('100%').height(160)
  }
}.onScrollIndex((start: number, end: number) => {
  if (end >= this.toBeSent.length - 1) {
    this.isLoading = true;
    setTimeout(() => {
      for (let i = 0; i < 10; i++) {
        const item: toBeSentData =  {
          id: `i`,
          upDateTime: 'Last Updated:2025/03/14',
          stateDateTime: '',
          title: 'To Matthew Riley',
          status: 6,
          list:[],
        }
        this.toBeSent.push(item);
      }
      this.isLoading = false;
    }, 700)
  }
})
.scrollBar(BarState.Off)
.edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })

B界面代码展示没有问题

aboutToAppear(): void { this.item = HMRouterMgr.getCurrentParam() as toBeSentData }获取到的前面列表内容并且可以展示

B界面传递出去

Image($r('[base].media.edit')).width(20).aspectRatio(1) 
.onClick(()=>{
 HMRouterMgr.push({ pageUrl: PAGE_PATH.recipient_designation, param:{ here:'Edit Recipient Information', item:this.item } })
})

c界面

export interface ResetPasswordOne{
  here:string;
}

export interface bucketListEdit{
  here:ResetPasswordOne,
  item:bucketList
}

export interface ListItem {
  id:string;
  text: string;
  image:string;
}

export interface CustomListItem {
  id:string;
  text: string;
  image: string;
}

export interface toBeSentData {
  id: string;
  upDateTime: string;
  stateDateTime: string;
  title: string;
  status: number;
  list: ListItem[];
}

数据

@Local toBeSent:toBeSentData[]=[
  {
    id:'01',
    upDateTime: 'Last Updated :2025/04/03',
    stateDateTime: '',
    title: 'To Charlotte Smith',
    status: 1,
    list:[
      {
        id:'01',
        text:"Dear my love If you're reading this, it means I'm no longer with you. Please know that my love for you will never fade, no matter where l am. You were the greatest gift in my life, and every moment we shared will forever be my most cherished memory.",
        image:'[base].media.test1'
      },
      {
        id:'02',
        text:"Be strong, as l know you are, and always remember that l am with you in spirit.",
        image:'[base].media.test2'
      },
    ],
  },
  {
    id:'02',
    upDateTime: 'Last Updated :2025/04/03',
    stateDateTime: '',
    title: 'To luna Smith',
    status: 2,
    list:[],
  },
  {
    id:'03',
    upDateTime: 'Last Updated:2025/03/14',
    stateDateTime: '',
    title: 'To Alan White',
    status: 3,
    list:[],
  },
  {
    id:'04',
    upDateTime: 'Last Updated:2025/03/14',
    stateDateTime: '',
    title: 'To William White',
    status: 4,
    list:[],
  },
  {
    id:'05',
    upDateTime: 'Last Updated:2025/03/14',
    stateDateTime: '',
    title: 'To James Brown',
    status: 5,
    list:[],
  },
  {
    id:'06',
    upDateTime: 'Last Updated:2025/03/14',
    stateDateTime: '',
    title: 'To Matthew Riley',
    status: 6,
    list:[],
  },
]

更多关于HarmonyOS 鸿蒙Next中HMRouter传参问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在鸿蒙Next中,HMRouter传参可通过以下方式实现:

  1. 使用parameters属性传递基本类型参数:
router.pushUrl({
  url: 'pages/TargetPage',
  parameters: {id: '123', name: 'test'}
})
  1. 目标页面通过router.getParams()获取参数:
onPageShow() {
  const params = router.getParams()
  let id = params['id']
}
  1. 传递对象参数需先序列化为JSON字符串,接收方再反序列化。

  2. 注意参数总长度不超过100KB。

更多关于HarmonyOS 鸿蒙Next中HMRouter传参问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


从代码来看,HMRouter传参在A→B正常,但B→C时item内容无法展示的问题,可能是由于参数序列化导致的。以下是关键分析点:

  1. 在B界面传递参数时,结构是{here:'Edit...', item:this.item},但C界面定义的bucketListEdit接口要求hereResetPasswordOne类型(需包含string属性),而实际传递的是字符串,这可能导致类型不匹配。

  2. 检查C界面aboutToAppear中的参数获取方式:

    this.bucketListEdit = HMRouterMgr.getCurrentParam() as bucketListEdit
    

    如果参数结构与接口定义不完全匹配,类型断言可能导致数据丢失。建议改为:

    const param = HMRouterMgr.getCurrentParam();
    if (param && 'item' in param) {
      this.bucketListEdit = param as bucketListEdit;
    }
    
  3. 确保在B→C传递时item对象是可序列化的,特别是list数组中的复杂对象。可以尝试在B界面传递前先深拷贝item对象:

    HMRouterMgr.push({
      pageUrl: PAGE_PATH.recipient_designation,
      param: {
        here: { here: 'Edit Recipient Information' }, // 匹配接口定义
        item: JSON.parse(JSON.stringify(this.item)) // 深拷贝
      }
    })
    
  4. 在C界面使用时检查数据完整性:

    console.log(JSON.stringify(this.bucketListEdit)); // 查看完整数据结构
    
回到顶部