鸿蒙 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)
  }
}
相关推荐
苗俊祥17 分钟前
OpenHarmony 7.0 源码走读 10|IPC 与 SAMgr:跨进程通信的地基
系统架构·鸿蒙·源码分析·openharmony·7.0
NJPJI_Yang1 小时前
鸿蒙操作系统使用 Navigation 实现页面路由与栈管理实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
程序猿追1 小时前
HarmonyOS 7 饮水倒计时归零以后,怎样核对系统收到提醒
华为·harmonyos
xiongmosy2 小时前
越过“规模临界点”之后:余承东给鸿蒙定的下一站,是“走向世界”
华为·harmonyos
程序猿追3 小时前
HarmonyOS 7 记账图表里,金额没变的交通条为什么变短了
华为·harmonyos
万物智能信息科技4 小时前
CAN总线协议解析和驱动开发—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
驱动开发·嵌入式硬件·华为·开源·harmonyos·鸿蒙
熊猫钓鱼>_>9 小时前
从零手写 2048:HarmonyOS ArkTS 声明式游戏的工程化实战【鸿蒙心迹】
华为·ai·华为云·ai编程·harmonyos·openharmony·鸿蒙心迹
程序猿追14 小时前
HarmonyOS 7 ArkUI 四则计算器:优先级、括号与除零修正
华为·harmonyos
leon_teacher17 小时前
HarmonyOS 7 Health Service Kit 实战:打通手表健康数据授权
华为·harmonyos
NJPJI_Yang17 小时前
鸿蒙操作系统使用 Gesture 实现手势识别与冲突处理实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos