鸿蒙 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)
  }
}
相关推荐
向哆哆31 分钟前
Flutter × OpenHarmony 跨端开发:高校四六级报名管理系统中的“常见问题”模块实现解析
flutter·开源·鸿蒙·openharmony·开源鸿蒙
小哥Mark1 小时前
一篇验证Flutter框架核心接口在鸿蒙应用中的可能性
flutter·华为·harmonyos
飞羽殇情1 小时前
基于React Native鸿蒙跨平台实现的电商客服咨询系统,支持在线客服、AI助手和电话咨询三种方式,并实现了问题分类、智能快捷回复等功能
react native·react.js·华为·harmonyos
酣大智2 小时前
FTP--文件传输协议
运维·网络·网络协议·tcp/ip·华为
weixin_443290692 小时前
【华为HCIA路由交换认证指南】第五章 静态路由
网络·华为·智能路由器
weixin_443290692 小时前
【华为HCIA路由交换认证指南】第六章 动态路由
网络·华为·智能路由器
前端世界2 小时前
鸿蒙应用如何集成第三方 SDK?真实项目中的完整实践
华为·harmonyos
人工智能知识库3 小时前
华为HCIP-Datacom H12-821题库(带详细解析)
华为·hcip-datacom·h12-821·华为数通·h12-821题库
以太浮标3 小时前
华为eNSP模拟器综合实验之- 前缀列表(IP Prefix-list --匹配和控制路由信息的强大工具)
tcp/ip·华为·智能路由器·list
今儿敲了吗12 小时前
鸿蒙开发第一章学习笔记
笔记·学习·鸿蒙