鸿蒙 next 5.0 版本页面跳转传参 && 接受参数 ,,接受的时候 要先定义接受参数的类型, 代码可以直接CV使用 [教程]

1, 先看效果

2, 先准备好两个页面

index 页面 传递参数

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

@Entry
@Component
struct Index {

  @State
  list: string[] = ['星期一', '星期二','星期三', '星期四','星期五']

  @State
  activeIndex: number = 0

  build() {
    Row() {
      Column({ space: 10 }) {
        ForEach(this.list, (item: string, index: number) => {
          Text(item)
            .width('100%')
            .height(50)
            .textAlign(TextAlign.Center)
            .fontSize(16)
            .backgroundColor(this.activeIndex === index ? Color.White : Color.Orange)
            .onClick(()=>{
              this.activeIndex = index
              console.log('111',this.activeIndex)
            })
        })
        Row(){
          Text('跳转传参')
            .onClick(()=>{
              router.pushUrl({
                url:'pages/one',
                params: {value:'test'}
              })
            })
        }
      }
      .width(90)
    }
    .padding(15)
    .backgroundColor(Color.Pink)
    .width('100%')
    .alignItems(VerticalAlign.Center)
  }
}

3, 接受参数

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

// 声明接受参数的类型
interface PARAMS {
  value: string
}

@Entry
@Component
struct one {

  @State
   paramsValue: PARAMS = {
    value: ''
  }

  aboutToAppear() {
    // as 转换一下变成我们要接受的类型
    this.paramsValue = router.getParams() as PARAMS

    // 控制台打印
    console.log(`111, ${JSON.stringify(this.paramsValue)}`)

    // 提示传过来的内容
    promptAction.showToast({message: this.paramsValue.value})
  }

  build() {
    Column(){
      Text('55555555555')

      // 显示传的参数内容
      Text(this.paramsValue.value)

    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
相关推荐
●VON6 分钟前
DeepSeek-V3.2 模型在 OpenJiuWen 中的部署实践
学习·华为·von·openjiuwen
liuhaikang41 分钟前
【鸿蒙HarmonyOS Next App实战开发】古诗书院 应用技术实践
华为·harmonyos
哈哈你是真的厉害1 小时前
Windows系统通过wsl Ubuntu24.04本地安装OpenJiuwen Studio的完整安装教程
人工智能·华为·ai·大模型·agent·智能体·openjiuwen
胡琦博客1 小时前
基于开发者空间鸿蒙云手机体验鸿蒙开发
华为·智能手机·harmonyos
kirk_wang2 小时前
Flutter `audio_service` 在鸿蒙端的后台音频服务适配实践
flutter·移动开发·跨平台·arkts·鸿蒙
namehao46513 小时前
从HarmonyOS基础认证到赋能资源建设:我的鸿蒙开发进阶之路
华为·harmonyos
航Hang*15 小时前
第十五章:网络系统建设与运维(高级)—— 总复习
网络·华为·ensp·期末·复习
彭不懂赶紧问18 小时前
鸿蒙NEXT开发浅进阶到精通15:从零搭建Navigation路由框架
前端·笔记·harmonyos·鸿蒙
爸爸6192 天前
Flutter跨平台开发:Fullscreen 在鸿蒙系统上的使用指南
flutter·华为·harmonyos
C雨后彩虹2 天前
猜密码问题
java·数据结构·算法·华为·面试