鸿蒙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

未完,待续

相关推荐
ChinaDragonDreamer6 天前
HarmonyOS:知识点总结(一)
harmonyos·鸿蒙
加农炮手Jinx6 天前
Flutter for OpenHarmony 实战:JWT — 构建安全的无状态认证中心
网络·flutter·华为·harmonyos·鸿蒙
雷帝木木6 天前
Flutter for OpenHarmony:Flutter 三方库 money2 — 坚不可摧的鸿蒙金融核心组件
网络·flutter·http·华为·金融·harmonyos·鸿蒙
特立独行的猫a7 天前
uniapp-x的HarmonyOS鸿蒙应用开发:tabbar底部导航栏的实现
华为·uni-app·harmonyos·鸿蒙·uniapp-x
●VON7 天前
HarmonyOS应用开发实战(基础篇)Day10 -《鸿蒙网络请求实战》
网络·学习·华为·harmonyos·鸿蒙·von
●VON8 天前
HarmonyOS应用开发实战(基础篇)Day08-《构建布局详解上》
华为·harmonyos·鸿蒙·von
加农炮手Jinx10 天前
Flutter for OpenHarmony 实战:疯狂头像 App(三)— 复合动画与交互反馈 — 让 UI 跃动起来
flutter·ui·交互·harmonyos·鸿蒙
_waylau11 天前
鸿蒙架构师修炼之道-架构师设计思维特点
华为·架构·架构师·harmonyos·鸿蒙·鸿蒙系统
Betelgeuse7611 天前
【Flutter For OpenHarmony】 项目结项复盘
华为·交互·开源软件·鸿蒙
ITUnicorn12 天前
【HarmonyOS 6】进度组件实战:打造精美的数据可视化
华为·harmonyos·arkts·鸿蒙·harmonyos6