【鸿蒙开发】使用HMRouter路由的使用

HMrouter

HMRouter作为应用内页面跳转场景解决方案,为开发者提供了功能完备、高效易用的路由框架。

HMRouter底层对系统Navigation进行封装,集成了NavigationNavDestinationNavPathStack的系统能力,提供了可复用的路由拦截、页面生命周期、自定义转场动画,并且在跳转传参、额外的生命周期、服务型路由方面对系统能力进行了扩展,同时开发者可以高效的将历史代码中的Navigation组件接入到HMRouter框架中。

目的是让开发者在开发过程中减少模板代码,降低拦截器、自定义转场动画、组件感知页面生命周期等高频开发场景的实现复杂度,帮助开发者更好的实现路由与业务模块间的解耦。

GitCode地址

安装

DevEco Studio终端中使用命令(ohpm命令集成在终端,如果想要在全局都使用,得去DevEco Studio安装目录寻找ohpm的地址并配置path)。

bash 复制代码
ohpm install @hadss/hmrouter

在根目录下的hvigor/hvigor-config.json文件中添加, 注意这里@hadss/hmrouter-plugin的版本需要和@hadss/hmrouter的版本一致

json 复制代码
"dependencies": {
  "@hadss/hmrouter-plugin": "^1.1.0-beta.0" 
},

找到entry/hvigorfile.ts 文件,添加

ts 复制代码
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import { hapPlugin } from '@hadss/hmrouter-plugin';

export default {
  system: hapTasks,
  plugins: [hapPlugin()] 
}

onCreate事件中初始化

kotlin 复制代码
HMRouterMgr.openLog('INFO');
HMRouterMgr.init({
  context: this.context,
});

使用HMNavigation定义入口(类似于vuerouter-view

scala 复制代码
// page/Index.ets

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

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

  build() {
    Row() {
      HMNavigation({
        navigationId: 'Index',
        homePageUrl: 'Main',
        options: {
          standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
          dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
          modifier: this.modifier,
        },
      })
    }
    .width('100%')
    .height('100%');
  }
}

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

使用@HMRouter定义页面

typescript 复制代码
import { HMRouter, HMRouterMgr } from '@hadss/hmrouter';

@HMRouter({
  pageUrl: 'RemoveList',
})
@Component
export struct RemoveList {
  build() {
    Column() {
      Text('RemoveList');
    };
  }
}

以上就HMRouter的基本全套流程就完成了

我在项目中的调整

目前以PC项目为例

布局调整

Index.ets中调整HMNavigation的位置

php 复制代码
Row() {
  Column() {
  }
  .width(200)
  .height('100%')
  .backgroundColor('#eee');

  HMNavigation({
    navigationId: 'Index',
    homePageUrl: 'Main',
    options: {
      standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
      dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
      modifier: this.modifier,
    },
  })
    .layoutWeight(1);

}
.width('100%')
.height('100%');

这样在Column中便利自己的nav列表,添加点击跳转事件

scss 复制代码
routers: RouterConfig[] = [{
  router: 'Main',
  name: '今日',
}, {
  router: 'RemoveList',
  name: '删除列表',
}];


ForEach(this.routers, (item: RouterConfig) => {
  Text(item.name)
    .lineHeight(30)
    .onClick(() => {
      const list = HMRouterMgr.getPathStack('Index');
      console.log(list?.size().toString());
      HMRouterMgr.push({
        pageUrl: item.router,
      });
    });
});

完成后的简易效果

相关推荐
MariaH15 分钟前
git rebase的使用
前端
_柳青杨15 分钟前
深入理解 JavaScript 事件循环
前端·javascript
阡陌Jony15 分钟前
关于前端性能优化的一些问题:
前端
用户600071819101 小时前
【翻译】简化 TSRX
前端
IT乐手2 小时前
佛德角逼平西班牙,国足还有啥借口?
前端
JustHappy3 小时前
我汇总了身边朋友的经历才发现,其实第一份实习是最难找的......
前端·后端·面试
星栈3 小时前
Dioxus 的响应式系统:`Signal`、`Memo`、`Effect` 和异步状态到底该怎么分工
前端·前端框架
yingyima3 小时前
Java 正则表达式:比你想象的更强大
前端
yuanyxh6 小时前
macOS 应用 - 纯对话生成
前端·macos·ai编程
大家的林语冰6 小时前
ES5 凉凉,Babel 8 正式发布,默认不再编译为 ES5 和 CJS......
前端·javascript·前端工程化