鸿蒙 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)
  }
}
相关推荐
梧桐ty32 分钟前
鸿蒙生态下的跨平台框架选型指南:Flutter vs React Native vs uni-app
flutter·华为·harmonyos
码力码力我爱你1 小时前
HarmonyOS DevEco Studio的使用练习题
华为·harmonyos
Neolnfra1 小时前
华为中小型企业网络建设
网络·华为·毕业设计·ensp代做
晚风(●•σ )1 小时前
【华为 ICT & HCIA & eNSP 习题汇总】——题目集26
网络·计算机网络·华为
yenggd1 小时前
华为SR-MPLS TE跨域(E2E)配置案例
华为
1 小时前
鸿蒙——布局——线性布局
华为·harmonyos
萌虎不虎12 小时前
【在鸿蒙系统中实现录制视频预览功能】
华为·音视频·harmonyos
m0_6855350812 小时前
Zemax 车载前视ADAS镜头
华为·光学·光学设计·光学工程·镜头设计
萌虎不虎15 小时前
【鸿蒙ETS中WebSocket使用说明】
websocket·华为·harmonyos
子榆.19 小时前
Flutter 与开源鸿蒙(OpenHarmony)国际化与无障碍适配指南:打造真正包容的跨平台应用
flutter·华为·开源·harmonyos