温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenHarmony菜单如何跳转

发布时间:2026-09-01 05:30:06 来源:亿速云 阅读:113 作者:小樊 栏目:软件技术

在 OpenHarmony(ArkUI,eTS/ArkTS) 中,“菜单跳转”通常有以下几种常见场景,下面分别说明:


一、页面(Ability / Page)之间跳转

1. 使用 router 跳转(最常见)

适用于 Stage 模型 / FA 模型页面跳转

import router from '@ohos.router'

// 跳转
router.pushUrl({
  url: 'pages/TargetPage'
})

// 带参数跳转
router.pushUrl({
  url: 'pages/TargetPage',
  params: {
    id: 1001
  }
})

目标页面接收参数:

import router from '@ohos.router'

@Entry
@Component
struct TargetPage {
  aboutToAppear() {
    const params = router.getParams()
    console.info('params:', JSON.stringify(params))
  }

  build() {
    // ...
  }
}

返回上一页:

router.back()

二、菜单(Menu)点击跳转

1. 使用 Menu + MenuItem

@Entry
@Component
struct Index {
  @State showMenu: boolean = false

  build() {
    Column() {
      Button('打开菜单')
        .onClick(() => {
          this.showMenu = true
        })
        .bindMenu([
          {
            value: '跳转页面A',
            action: () => {
              router.pushUrl({ url: 'pages/PageA' })
            }
          },
          {
            value: '跳转页面B',
            action: () => {
              router.pushUrl({ url: 'pages/PageB' })
            }
          }
        ])
    }
  }
}

三、Navigation 导航跳转(推荐新项目)

1. 使用 Navigation + NavPathStack

// 主页面
@Entry
@Component
struct MainPage {
  @Provide navPathStack: NavPathStack = new NavPathStack()

  build() {
    Navigation(this.navPathStack) {
      Column() {
        Button('去详情页')
          .onClick(() => {
            this.navPathStack.pushPath({ name: 'Detail' })
          })
      }
    }
  }
}
// 详情页
@Component
struct DetailPage {
  @Consume navPathStack: NavPathStack

  build() {
    Column() {
      Text('详情页')
      Button('返回')
        .onClick(() => {
          this.navPathStack.pop()
        })
    }
  }
}

四、Ability 级别跳转(跨模块 / 跨应用)

import featureAbility from '@ohos.ability.featureAbility'

featureAbility.startAbility({
  want: {
    bundleName: 'com.example.app',
    abilityName: 'com.example.app.MainAbility'
  }
})

五、常见选择建议

场景 推荐方式
页面内菜单跳转 bindMenu + router
多页面应用 Navigation
跨 Ability startAbility

如果你能说明:

  • 使用的是 OpenHarmony 哪个版本
  • FA 还是 Stage 模型
  • 是 系统菜单 / 自定义菜单 / 设置菜单

我可以给你更精确的代码示例。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手