HarmonyOS 怎么跨模块路由跳转界面?

怎么在依赖的user模块里调用entry模块组件

我的entry依赖于user模块,怎么在user组件里点击按钮跳转到entry里面的组件

阅读 761
1 个回答

共享har或者hsp包的跳转,使用router.replaceNamedRoute路由名称跳转(共享包里面的页面,不需要再main\_pages里面配置)。

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-routing-V5

【entry跳har/hsp】

import router from ‘@ohos.router’;
import(‘library/src/main/ets/pages/Index’) // 引入共享包中的命名路由页面,如果存在红色波浪线,不影响编译打包
@Entry
@Component
struct Index {
 build() {
  Text(‘点击’).onclick(()=>{
   router.pushNamedRoute({
    name: ‘harA’
   })
  })
 }
}

【harA跳转harB】

@Entry({ routeName : ‘harA’ })
@Component
export struct HarA {
}

@Entry({ routeName : ‘harA’ })
@Component
export struct HarA {
 build() {
  Text(‘点击’).onclick(()=>{
   router.pushNamedRoute({
    name: ‘harB’
   })
  })
 }
}

@Entry({ routeName : ‘harB’ })
@Component
export struct HarB {
}

由于router不演进,建议采用Navgation作为导航容器:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-navigation-navigation-V5

不同模块(har/hsp)页面跳转依赖问题,推荐使用Navigation去实现多模块路由管理和模块间解耦。

参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-navigation-navigation-V5

navigation跨包动态路由:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/ui/arkts-navigation-navigation.md\#%E8%B7%A8%E5%8C%85%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进