鸿蒙HarmonyOS之跳转页面并传递参数的方法

一、使用router路由跳转页面并携带参数

1、导入库

复制代码
import router from '@kit.ArkUI';

2、跳转页面并携带参数

复制代码
export class ActivityResponse {
......
}

(1)携带多个参数

复制代码
router.pushUrl({
      url: 'pages/NextPage',
      params: {
            name: '名字', //string类型
            avator: '头像', //string类型
            size: 大小, //number类型
            responsedata: '某个对象数据' //ActivityResponse类型
      }
})

NextPage页解析:

复制代码
private responseData: ActivityResponse = new ActivityResponse();
private name: string = ''
private avator: string = ''
private size: number = 0
  
......
  
let params = router.getParams() as Record<string, number | string | ActivityResponse>;
this.responseData= params.result as ActivityResponse;
this.name= params.uri as string;
this.avator= params.infoW as string;
this.size= params.infoH as number;

(2)携带一个参数,参数是类对象

在参数过多的情况下,可以将传递的参数封装为类,以对象方式传递更方便接收解析

复制代码
private curr_data: ActivityResponse = new ActivityResponse();
......
router.pushUrl({
	url: 'pages/NextPage',
    params: this.curr_data
},router.RouterMode.Standard,(err)=>{
    if (err) {
      console.error(`跳转页面失败, code is ${err.code}, message is ${err.message}`);
      return;
    }
  })

NextPage页解析:

复制代码
this.params = router.getParams() as ActivityResponse;

二、使用navigation

未完,待续

相关推荐
黑鲨吃西瓜3 小时前
鸿蒙通用模块
harmonyos·鸿蒙
math_hongfan1 天前
鸿蒙离线数据缓存高级架构:弱网预加载/离线数据优先级/同步冲突解决/上线后数据合并策略
学习·缓存·华为·架构·harmonyos·鸿蒙
math_hongfan2 天前
鸿蒙企业级数据存储高级架构:从读写分离到冷热数据分层/归档策略/数据生命周期管理最佳实践
人工智能·学习·华为·架构·harmonyos·鸿蒙
math_hongfan2 天前
鸿蒙存储异常高级排查:文件损坏检测/数据恢复/读写失败重试/磁盘空间预警系统性根治方案
学习·华为·harmonyos·鸿蒙
math_hongfan2 天前
鸿蒙存储碎片高级清理机制:数据库Vacuum/文件碎片整理/缓存过期清理/主动空间回收策略
jvm·数据库·学习·缓存·华为·harmonyos·鸿蒙
OH_TPC3 天前
【鸿蒙优选三方库】@ohos/grpc:在 HarmonyOS 上像调本地方法一样调远程服务
华为·harmonyos·鸿蒙
math_hongfan3 天前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
math_hongfan3 天前
鸿蒙AI权限与隐私保护高级:智能数据脱敏/端侧处理优先/数据最小化采集/隐私计算技术栈
人工智能·学习·华为·harmonyos·鸿蒙
math_hongfan3 天前
鸿蒙多模态AI交互高级:图文+语音+手势融合交互/多模态大模型端侧适配/跨模态检索高阶实战
人工智能·学习·华为·交互·语音识别·harmonyos·鸿蒙
math_hongfan3 天前
鸿蒙AI应用性能高级评测:推理延迟/内存占用/功耗/准确率四维指标评测体系与极致调优方案
人工智能·学习·华为·harmonyos·鸿蒙