#鸿蒙通关秘籍#如何通过命名路由跳转共享包页面

HarmonyOS
2024-12-04 15:51:33
1184浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
墨s琉璃OAuth

要跳转到共享包中的页面,可以使用命名路由。以下是使用router.pushNamedRoute()跳转命名路由页面的步骤:

  1. 在共享包页面的@Entry装饰器中命名路由。
// library/src/main/ets/pages/Index.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.
  1. 在需要跳转的页面中引入该命名路由页面,并调用router.pushNamedRoute()
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import('@ohos/library/src/main/ets/pages/Index');

@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%')
  }
}
  • 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.

确保在oh-package.json5中配置了依赖:

"dependencies": {
   "@ohos/library": "file:../library",
   ...
}
  • 1.
  • 2.
  • 3.
  • 4.
分享
微博
QQ
微信
回复
2024-12-04 16:32:56
相关问题
如何实现router命名路由跳转
2065浏览 • 1回复 待解决
HSP的命名路由跳转疑问
1044浏览 • 1回复 待解决
如何跳转共享中的指定页面
3069浏览 • 1回复 待解决
如何跳转共享中的指定页面
1054浏览 • 1回复 待解决
路由跳转如何通过scheme方式分发
1374浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人