HarmonyOS hap中不能使用命名路由吗

1、hap中不能使用命名路由吗,我在hap中使用命名路由的方式没有调用成功,所以不知道是我写法的问题还是不支持

2、@Entry 中的name 是否支持变量呢

HarmonyOS
2024-12-25 07:39:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

在同一个hap下,页面之间的跳转可以用命名路由的方式

可见以下案例:

在页面src/main/ets/pages/Index.ets中

import router from '@ohos.router';
import { BusinessError } from '@ohos.base';
import('./myPage');  // 引入共享包中的命名路由页面
@Entry
@Component
struct Index {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('Hello World')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .margin({ top: 20 })
        .backgroundColor('#ccc')
        .onClick(() => { // 点击跳转到其他共享包中的页面
          try {
            router.pushNamedRoute({
              name: 'myPage',
              params: {
                data1: 'message',
                data2: {
                  data3: [123, 456, 789]
                }
              }
            })
          } catch (err) {
            let message = (err as BusinessError).message
            let code = (err as BusinessError).code
            console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
          }
        })
    }
    .width('100%')
    .height('100%')
  }
}

//在页面src/main/ets/pages/myPage.ets中
@Entry({ routeName: 'myPage' })
@Component
export struct MyComponent {
  build() {
    Row() {
      Column() {
        Text('Library Page')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-12-25 10:58:48


相关问题
HarmonyOS .ets文件不能使用方法重载
387浏览 • 1回复 待解决
xComponet示例代码不能使用
1422浏览 • 1回复 待解决
如何实现router命名路由跳转
1740浏览 • 1回复 待解决
HSP的命名路由跳转疑问
737浏览 • 1回复 待解决
HarmonyOS 能使用Charles代理抓包
525浏览 • 1回复 待解决
ts 声明式开发不能使用js的getApp()
5004浏览 • 1回复 待解决