鸿蒙 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)
  }
}
相关推荐
向哆哆1 小时前
构建环保之旅:基于 Flutter × OpenHarmony 的垃圾回收个人成就应用
flutter·开源·鸿蒙·openharmony·开源鸿蒙
zhousg10071 小时前
我在鸿蒙【生态市场】发布的活跃预览组件上架了
华为·harmonyos
俩毛豆2 小时前
HarmonyOS APP开发-一文说清基础类型数据的非预期输入转换与兜底-《精通HarmonyOS NEXT :鸿蒙App开发入门与项目化实战》读者福利
华为·harmonyos
向哆哆2 小时前
Flutter × OpenHarmony 跨端实战:垃圾分类应用页面架构与数据结构设计详解
数据结构·flutter·开源·鸿蒙·openharmony
lbb 小魔仙2 小时前
【Harmonyos】开源鸿蒙跨平台训练营DAY8:Flutter鸿蒙开发指南获取轮播图数据
flutter·华为·harmonyos
以太浮标3 小时前
华为eNSP模拟器综合实验之-多协议标签交换 MPLS (Multi-Protocol Label Switching)
华为·智能路由器
向哆哆3 小时前
跨端开发实践:Flutter × OpenHarmony 构建垃圾回收分类知识区域
flutter·开源·鸿蒙·openharmony
熊猫钓鱼>_>3 小时前
【开源鸿蒙跨平台开发先锋训练营】Day 9:鸿蒙跨平台Tab 开发问题与列表操作难点复盘
react native·华为·开源·tab·harmonyos·rn·hvigor
Goway_Hui3 小时前
【开源鸿蒙跨平台开发--KuiklyUI--03】KuiklyUI 入门实战:从零打造高性能跨平台 Todo 应用
华为·开源·harmonyos·kuikly
晚霞的不甘4 小时前
Flutter for OpenHarmony《智慧字典》中的沉浸式学习:成语测试与填空练习等功能详解
学习·flutter·ui·信息可视化·前端框架·鸿蒙