IT评测·应用市场-qidao123.com

标题: 鸿蒙实战:页面跳转传参 [打印本页]

作者: 冬雨财经    时间: 2025-1-13 14:40
标题: 鸿蒙实战:页面跳转传参
1. 实战概述


2. 实现步骤

2.1 创建鸿蒙项目


2.2 编写首页代码


  1. // 导入router模块
  2. import router from '@ohos.router'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   @State message: string = 'Index页面';
  7.   build() {
  8.     Row() {
  9.       Column() {
  10.         Text(this.message)
  11.           .fontSize(40)
  12.           .fontWeight(FontWeight.Bold)
  13.           .foregroundColor(Color.Yellow)
  14.           .margin('10')
  15.         // 添加按钮,触发跳转
  16.         Button('跳转')
  17.           .fontSize(40)
  18.           .width(150)
  19.           .height(70)
  20.           .backgroundColor('#44dd22')
  21.           .foregroundColor('#ffffff')
  22.           .onClick(() => {
  23.             router.pushUrl({
  24.               url: 'pages/Second',
  25.               params: {
  26.                 id: '20240101',
  27.                 name: '陈燕文',
  28.                 gender: '女',
  29.                 age: 19,
  30.                 major: '软件技术专业',
  31.                 class: '2024软件1班',
  32.                 telephone: '15893451170'
  33.               }
  34.             })
  35.           })
  36.       }
  37.       .width('100%')
  38.     }
  39.     .height('100%')
  40.     .backgroundColor('#00662F')
  41.   }
  42. }
复制代码

2.3 新建第二个页面


  1. // 导入router模块
  2. import router from '@ohos.router'
  3. @Entry
  4. @Component
  5. struct Second {
  6.   @State message: string = 'Second页面';
  7.   @State student: string = '';
  8.   aboutToAppear(): void {
  9.     let record = router.getParams() as Record<string, string>;
  10.     this.student = '学号:' + record['id'] + '\n'
  11.       + '姓名:' + record['name'] + '\n'
  12.       + '性别:' + record['gender'] + '\n'
  13.       + '年龄:' + record['age'] + '\n'
  14.       + '专业:' + record['major'] + '\n'
  15.       + '班级:' + record['class'] + '\n'
  16.       + '手机:' + record['telephone'];
  17.   }
  18.   build() {
  19.     Row() {
  20.       Column() {
  21.         Text(this.message)
  22.           .fontSize(40)
  23.           .fontWeight(FontWeight.Bold)
  24.           .foregroundColor(Color.Yellow)
  25.           .margin('10')
  26.         // 显示传参的内容
  27.         Text(this.student)
  28.           .fontSize(30)
  29.           .fontColor(Color.Red)
  30.           .margin('20')
  31.         // 添加按钮,触发返回
  32.         Button('返回')
  33.           .fontSize(40)
  34.           .width(150)
  35.           .height(70)
  36.           .backgroundColor('#44dd22')
  37.           .foregroundColor('#ffffff')
  38.           .onClick(() => {
  39.             router.back();
  40.           })
  41.       }
  42.       .width('100%')
  43.     }
  44.     .height('100%')
  45.     .backgroundColor('#00008B')
  46.   }
  47. }
复制代码

3. 测试结果


4. 实战总结



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4