在 OpenHarmony(ArkUI,eTS/ArkTS) 中,“菜单跳转”通常有以下几种常见场景,下面分别说明:
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 + 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 + 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()
})
}
}
}
import featureAbility from '@ohos.ability.featureAbility'
featureAbility.startAbility({
want: {
bundleName: 'com.example.app',
abilityName: 'com.example.app.MainAbility'
}
})
| 场景 | 推荐方式 |
|---|---|
| 页面内菜单跳转 | bindMenu + router |
| 多页面应用 | Navigation |
| 跨 Ability | startAbility |
如果你能说明:
我可以给你更精确的代码示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。