harmonyos的鸿蒙的跳转页面的部署参数传递

使用编译器环境,

harmonyOs 5.1

代码如下:

复制代码
Button("跳转到测试1")
        .onClick(()=>{
          router.pushUrl({
            url: "pages/test",
            params: { name: '鸿蒙ArkTS教程' } // 可携带参数

          })
            .then(() => {
              console.log('跳转成功')
            }).catch((err:Error) => {
            console.log('跳转失败:' + err.message)
          })
        })

携带参数"鸿蒙ArkTS教程"到下一页面。

下一页面test.ets代码如下:

复制代码
import router from '@ohos.router'

interface RouterParams {
  name?: string
}

@State private name: string = ''

  aboutToAppear() {
    // 🔹 接收参数
    // 🔹 接收参数(router 必须正确导入)
    const params: RouterParams = router.getParams() as RouterParams
    if (params && params.name) {
      this.name = params.name
    }
  }

在页面里的build加入:

复制代码
Text(`传递过来的参数:${this.name}`)
        .fontSize(20)
        .fontColor(Color.Black)

完整代码如下:

复制代码
import home from './daohanglan/home'  // 若在同一目录,用相对路径 './HomePage'
import router from '@ohos.router'

interface RouterParams {
  name?: string
}

@Entry
@Component
struct BottomNavExample {
  @State currentIndex: number = 0
  private tabs: string[] = ['首页', '购物车', '我的']
  @State private name: string = ''

  aboutToAppear() {
    // 🔹 接收参数
    // 🔹 接收参数(router 必须正确导入)
    const params: RouterParams = router.getParams() as RouterParams
    if (params && params.name) {
      this.name = params.name
    }
  }
  build() {
    Column() {
      Text(`传递过来的参数:${this.name}`)
        .fontSize(20)
        .fontColor(Color.Black)
      Column(){
        // 页面内容区域
        if (this.currentIndex === 0) {
          home()
        } else if (this.currentIndex === 1) {
          CartPage()
        } else {
          MinePage()
        }
      }
      .height('95%')

      // 底部导航栏
      Row({ space: 30 }) {
        ForEach(this.tabs, (tab:string, index) => {
          Column() {
            Text(tab)
              .fontSize(18)
              .fontColor(this.currentIndex === index ? Color.Blue : Color.Gray)
              .onClick(() => {
                this.currentIndex = index
              })
          }
          .width('33%')
          .alignItems(HorizontalAlign.Center)
        }, (tab:string) => tab)
      }
      .padding({ top: 12, bottom: 30 }) // ✅ 增加底部留白
      .border({ width: 5, color: '#ddd' })
      .justifyContent(FlexAlign.SpaceAround)
    }
    .height("100%")
    .justifyContent(FlexAlign.SpaceBetween)
  }
}
// ---------------- 子页面组件 ----------------
@Component
struct HomePage {
  build() {
    Column() {
      Text('🏠 这里是首页内容')
        .fontSize(22)
        .fontWeight(FontWeight.Bold)
    }

    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

@Component
struct CartPage {
  build() {
    Column() {
      Text('🛒 购物车页面')
        .fontSize(22)
        .fontWeight(FontWeight.Bold)
    }

    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

@Component
struct MinePage {
  build() {
    Column() {
      Text('👤 我的页面')
        .fontSize(22)
        .fontWeight(FontWeight.Bold)
    }

    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

运行效果:

相关推荐
特立独行的猫a1 小时前
Tauri 应用移植到 OpenHarmony/鸿蒙PC完整指南
华为·rust·harmonyos·tauri·移植·鸿蒙pc
互联网散修2 小时前
鸿蒙实战:文字放大镜精确跟随手指放大
华为·harmonyos
金启攻5 小时前
【鸿蒙应用开发实战·食光篇】第二篇:首页与菜系导航——圆形封面与美食榜单
华为·harmonyos
JohnnyDeng945 小时前
【鸿蒙】ArkUI 列表性能优化:LazyForEach 与组件复用深度解析
性能优化·harmonyos·arkts·鸿蒙·arkui
●VON6 小时前
AtomGit Flutter鸿蒙客户端:设置页面
flutter·华为·跨平台·harmonyos·鸿蒙
FrameNotWork6 小时前
HarmonyOS6.1 AI 模型管理架构设计与最佳实践
人工智能·harmonyos
wordbaby7 小时前
rn-cross-calendar:一个兼容 React 18/19、RN/RNOH 的跨平台日历组件
前端·react native·harmonyos
●VON7 小时前
AtomGit Flutter鸿蒙客户端:用户资料
flutter·华为·架构·跨平台·harmonyos·鸿蒙
风华圆舞8 小时前
Stage 模型下 Flutter 鸿蒙壳工程怎么理解
flutter·华为·harmonyos
●VON8 小时前
AtomGit Flutter鸿蒙客户端:数据模型
android·服务器·安全·flutter·harmonyos·鸿蒙