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_9197490312 小时前
华为鸿蒙免费外卖记录APP—小羊外卖
华为·harmonyos·鸿蒙
云端漫步19871 天前
HarmonyOS 互动卡片(Live Form)入门指南:双态架构与场景动效全景解析
华为·架构·harmonyos
OH_TPC1 天前
HarmonyOS APP开发---“绿管家“智能植物监测App,需要用到这个库
华为·harmonyos·鸿蒙
云端漫步19871 天前
HarmonyOS 互动卡片实战:快递卡片与睡眠卡片完整开发流程
华为·harmonyos
智塑未来1 天前
鸿蒙6.1隐私安心加倍:加密分享、星盾防诈、应用锁三重保护,守护到位
华为·harmonyos
byte轻骑兵1 天前
2026手机远程办公:向日葵、TeamViewer、ToDesk鸿蒙适配+传文件+AI审计功能对比
智能手机·harmonyos·todesk·teamviewer·手机远程办公
贾伟康1 天前
【知律|06】HarmonyOS ArkTS 法律分类实战:让民法、劳动、消费等入口可维护
harmonyos·arkts·arkui·分类设计·多设备
梦想不只是梦与想2 天前
鸿蒙 测试工具:DevEco Testing(一)
测试工具·harmonyos·testing
大锅盖12 天前
Web 工单要调用相机,第一步不是打开取景框,而是建立能力门禁
前端·数码相机·harmonyos
贾伟康2 天前
【华夏二十四节气|07】HarmonyOS 6.0.2(22) ArkTS 节气搜索实战:多字段匹配与四态闭环
移动开发·harmonyos·arkts·arkui·本地搜索