鸿蒙 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)
  }
}
相关推荐
程序员小刘2 小时前
HarmonyOS 5中UniApp的调试步骤
华为·uni-app·harmonyos
爱笑的眼睛116 小时前
HarmonyOS 组件复用 指南
华为·harmonyos·harmonyos next
fei_sun13 小时前
【华为开发者学堂】HarmonyOS
华为·harmonyos
何玺13 小时前
从Pura 80系列影像和鸿蒙AI融合看华为创新的“不可复制性”
人工智能·华为·harmonyos
Python当打之年16 小时前
【59 Pandas+Pyecharts | 淘宝华为手机商品数据分析可视化】
华为·智能手机·数据分析·pandas·数据可视化
程序员小刘17 小时前
HarmonyOS 5对React Native有哪些新特性?
react native·华为·harmonyos
程序员小刘18 小时前
HarmonyOS 5鸿蒙多端编译实战:从Android/iOS到HarmonyOS 5 的跨端迁移指南详
android·ios·华为·harmonyos
爱笑的眼睛111 天前
HarmonyOS 组件复用面试宝典 [特殊字符]
华为·面试·harmonyos·harmonyos next
我睡醒再说2 天前
HarmonyOS5 运动健康app(二):健康跑步(附代码)
华为·信息可视化·harmonyos·arkts·应用开发
枫叶丹42 天前
【HarmonyOS Next之旅】DevEco Studio使用指南(三十三) -> 构建任务
华为·harmonyos·deveco studio·harmonyos next