目录

鸿蒙路由-HMrouter-配置及使用一

鸿蒙路由 HMrouter 配置及使用一

1、学习链接

HMRouter地址

2、工程配置

下载安装

ohpm install @hadss/hmrouter

添加编译插件配置

https://i-blog.csdnimg.cn/direct/dc1a3f3c09b34a2d9ff226270b9d8188.png

在工程目录下的 build-profile.json5 中,配置 useNormalizedOHMUrl 属性为true (我这项目创建后默认就是true)

https://i-blog.csdnimg.cn/direct/9b541d80c1794d4c9bb538de9603b813.png

在使用到HMRouter的模块中引入路由编译插件,修改 hvigorfile.ts。

如果模块是Har则使用 harPlugin() , 模块是Hsp则使用 hspPlugin() , 模块是Hap则使用 hapPlugin()

https://i-blog.csdnimg.cn/direct/21bf6ca89d4d4a4899b8eb9aeb3bc8f8.png

3、开始使用

在UIAbility或者启动框架AppStartup中初始化路由框架

https://i-blog.csdnimg.cn/direct/f0b4d3ae2e4a4978a0222577d6a64da2.png

在模块入口配置一个 HMNavigationr容器并添加配置信息

import { HMDefaultGlobalAnimator, HMNavigation } from "@hadss/hmrouter";
import { AttributeUpdater } from "@kit.ArkUI";

@Entry
@Component
export struct Index {
  modifier: MyNavModifier = new MyNavModifier();

  build() {
    // @Entry中需要再套一层容器组件,Column或者Stack
    Column(){
      // 使用HMNavigation容器
      HMNavigation({
        navigationId: 'mainNavigation', homePageUrl: 'MainPage',
        options: {
          standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
          dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
          modifier: this.modifier
        }
      })
    }
    .height('100%')
    .width('100%')
  }
}

class MyNavModifier extends AttributeUpdater<NavigationAttribute> {
  initializeModifier(instance: NavigationAttribute): void {
    instance.hideNavBar(true);
  }
}

配置信息说明

https://i-blog.csdnimg.cn/direct/98960a9383c045e0b11ebdae17d785e7.png

创建默认的加载页面,这里需要注意是创建ArkTS File文件而不是创建Page

https://i-blog.csdnimg.cn/direct/7ba1f1975e5d4dbe830900617a7b9730.png

import { HMRouter } from "@hadss/hmrouter"


@HMRouter({
  pageUrl: "MainPage"
})
@Component
export struct MainPage {
  build() {
    Column(){
      Text("首页").fontSize(30).fontWeight(FontWeight.Bold)
    }
    .width('100%')
    .height('100%')
    .backgroundColor("#f4f5f9")
    .justifyContent(FlexAlign.Center)
  }
}

效果图

https://i-blog.csdnimg.cn/direct/2a8e45f6caba407cb5a8bcbcf7e725c4.png