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)
  }
}

运行效果:

相关推荐
2501_9209317018 小时前
React Native鸿蒙跨平台采用ScrollView的horizontal属性实现横向滚动实现特色游戏轮播和分类导航
javascript·react native·react.js·游戏·ecmascript·harmonyos
摘星编程20 小时前
React Native鸿蒙版:Drawer抽屉导航实现
react native·react.js·harmonyos
lbb 小魔仙21 小时前
【Harmonyos】开源鸿蒙跨平台训练营DAY9:获取分类数据并渲染
flutter·华为·harmonyos
mocoding21 小时前
Flutter 3D 翻转动画flip_card三方库在鸿蒙版天气预报卡片中的实战教程
flutter·3d·harmonyos
2501_920931701 天前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
C雨后彩虹1 天前
计算疫情扩散时间
java·数据结构·算法·华为·面试
24zhgjx-lxq1 天前
华为ensp:MSTP
网络·安全·华为·hcip·ensp
qq_177767371 天前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
烬头88211 天前
React Native鸿蒙跨平台应用实现了onCategoryPress等核心函数,用于处理用户交互和状态更新,通过计算已支出和剩余预算
前端·javascript·react native·react.js·ecmascript·交互·harmonyos
小雨青年1 天前
鸿蒙 HarmonyOS 6 | 系统能力 (06) 构建现代化通知体系 从基础消息到实况
华为·harmonyos