鸿蒙 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)
  }
}
相关推荐
李李李勃谦4 小时前
鸿蒙PC日志分析工具:实时监控、高亮显示与智能过滤
华为·harmonyos
maaath6 小时前
【maaath】Flutter for OpenHarmony 乐器学习应用开发实战
flutter·华为·harmonyos
数智顾问7 小时前
(123页PPT)华为流程管理体系精髓提炼(附下载方式)
运维·华为
李李李勃谦11 小时前
鸿蒙PCBI 报表工具:连接数据库与可视化报表生成
数据库·华为·交互·harmonyos
maaath11 小时前
【maaath】 Flutter for OpenHarmony 实战:电池优化应用开发指南
flutter·华为·harmonyos
aqi0012 小时前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony
智能化咨询13 小时前
(105页PPT)华为智慧供应链ISC+IT蓝图规划设计方案ISC+战略愿景与蓝图设计ISC+能力框架与架构设计实施路径(附下载方式)
华为
李李李勃谦13 小时前
鸿蒙PC配色方案工具:取色、配色生成与 CSS 导出
前端·css·华为·harmonyos
SmartBrain15 小时前
《资治通鉴》20 条智慧赋能企业经营管理
华为·架构·创业创新
条tiao条15 小时前
从静态到动态:鸿蒙 ArkTS 列表组件与状态装饰器实战
华为·harmonyos