鸿蒙 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)
  }
}
相关推荐
Magic-ZYJ8 小时前
HarmonyOS Release 构建安全:ArkGuard 混淆、签名配置与秘密管理
安全·华为·harmonyos·移动应用开发·心晴手记
见山是山-见水是水9 小时前
拆解HarmonyOS网络请求适配:原生鸿蒙页面的实现路径与调试方法
华为·harmonyos
lqj_本人11 小时前
鸿蒙跨端实战_QT后台切换导致OpenGLES上下文丢失:黑屏自救指南
qt·华为·harmonyos
OH_TPC12 小时前
HarmonyOS APP开发---“数据眼“健康追踪App,需要用到这个库
华为·harmonyos·鸿蒙
lilian23313 小时前
Harmony os 技术实战|拼豆制图49:五组分类视觉规则如何收口成一张配置表
华为·harmonyos
2501_9197490314 小时前
华为鸿蒙免费决策APP—小羊挠头
华为·harmonyos·鸿蒙
m0_7496902314 小时前
【寻迹校园 HarmonyOS NEXT 实战 37】一套 ArkUI 适配四档宽度:sm、md、lg、xl 响应式 Shell 实战
华为·harmonyos·arkui·navigation·响应式布局·多设备适配
RisunJan16 小时前
HarmonyOS 架构精读:从系统分层到应用模型
华为·架构·harmonyos
见山是山-见水是水16 小时前
从需求到页面:鸿蒙原生 Ability 与 ArkTS 交互的 ArkTS 原生实现
华为·harmonyos
见山是山-见水是水16 小时前
网络请求性能优化落地指南:让原生界面在模拟器里稳定运行
网络协议·华为·性能优化·harmonyos