鸿蒙路由参数传递

页面test.ets

代码如下:

复制代码
import router from '@ohos.router'
@Entry
@Component
struct Test {
  @State message: string = 'Hello World'
  @State username: string = 'hu'
  @State password: string = '1'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            router.pushUrl({
              url: "pages/mine/MinePage",
              params: {
                username: this.username,
                password: this.password
              }
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

页面MinePage.ets如下:

复制代码
import router from '@ohos.router'

@Entry
@Component
struct MinePage {
  @State username: string = ''
  @State password: string = ''

  aboutToAppear() {
    const params = router.getParams() as { username?: string; password?: string }
    this.username = params.username ?? ''
    this.password = params.password ?? ''
  }


  build() {
    Column() {
      Text(`欢迎你,${this.username}`)
      Text(`你的密码是:${this.password}`)
    }.padding(20)
  }
}

运行结果如下:

相关推荐
M-Robots echo8 小时前
M-CLAW,面向具身智能的机器人任务编排开发框架
机器人·ros·鸿蒙·开源社区·m-robots
M-Robots echo9 小时前
王成录:机器人产业下一个十年,拼的是 “系统级智能化” 而非单点算法堆叠
机器人·ros·鸿蒙·开源社区·m-robots·王成录
M-Robots echo15 小时前
M-Robots ROS 兼容适配方案,实现存量机器人项目低成本迁移
机器人·ros·鸿蒙·开源社区·m-robots
UnicornIT20 小时前
【HarmonyOS】时间管理类APP:做成“自适应“
ui·华为·harmonyos·鸿蒙
OH_TPC1 天前
HarmonyOS APP开发---“滤镜大师“图像处理App,需要用到这个库
java·图像处理·华为·harmonyos·鸿蒙
2501_919749031 天前
华为鸿蒙管理密码APP—小羊密码
华为·harmonyos·鸿蒙
笔触狂放2 天前
第2章 ArkTS(上)
华为·harmonyos·鸿蒙
M-Robots echo2 天前
王成录:黑客松不只是赛事,是开源机器人生态快速试错、挖掘创新的试验场
机器人·ros·鸿蒙·具身智能·开源社区·m-robots
OH_TPC2 天前
【鸿蒙优选三方库】@ohos/mpchart:让 HarmonyOS 应用的数据可视化开箱即用
华为·信息可视化·harmonyos·鸿蒙
轻口味3 天前
端侧 AI 赋能鸿蒙版 Obsidian:轻规划基于 HarmonyOS 7.0 图像超分特性的体验突破与开发实战
人工智能·华为·harmonyos·鸿蒙