【HarmonyOS】HMRouter使用详解(二)路由跳转

路由跳转


HMRouter中使用HMRouterMgr的静态方法push()和replace()来实现路由跳转。使用pop()方法来实现页面返回

  • push :目标页面不会替换当前页,而是插入页面栈。可以使用pop实现页面的返回操作。
  • replace:目标页面会替换当前页,并销毁当前页。这样可以释放当前页的资源,并且无法返回到当前页。
  • pop:返回页面栈的上一个页面,skipedLayerNumber 页面返回的层级数量,默认为0,表示返回上一级,1表示跳过一级页面返回
static push(pathInfo: HMRouterPathInfo, callback?: HMRouterPathCallback): void;

static replace(pathInfo: HMRouterPathInfo, callback?: HMRouterPathCallback): void;

static pop(pathInfo?: HMRouterPathInfo, skipedLayerNumber?: number): void;

跳转示例代码

push

              HMRouterMgr.push({
                navigationId: "mainNavigation",
                pageUrl: "TwoPage"
              })

replace

          HMRouterMgr.replace({
            navigationId: "mainNavigation",
            pageUrl: "ThreePage"
          })

携带参数跳转

              HMRouterMgr.push({
                navigationId: "mainNavigation",
                pageUrl: "TwoPage",
                param: new PageModel("张三", "12")
              })

PageModel

export class PageModel {
  Name?:string
  Age?:string
  constructor(Name: string, Age: string) {
    this.Name = Name
    this.Age = Age
  }
}

HomePage

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

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

  build() {
    // @Entry中需要再套一层容器组件,Column或者Stack
    Column() {
      // 使用HMNavigation容器
      HMNavigation({
        navigationId: 'mainNavigation', options: {
          standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
          dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
          modifier: this.modifier
        }
      }) {
        Column({ space: 20 }) {
          Button("TwoPage")
            .width("80%")
            .onClick(() => {
              HMRouterMgr.push({
                navigationId: "mainNavigation",
                pageUrl: "TwoPage"
              })
            })
          Button("TwoPageParam")
            .width("80%")
            .onClick(() => {
              HMRouterMgr.push({
                navigationId: "mainNavigation",
                pageUrl: "TwoPage",
                param: new PageModel("张三", "12")
              })
            })
        }
        .width('100%')
        .height('100%')
      }
    }
    .height('100%')
    .width('100%')
  }
}

class NavModifier extends AttributeUpdater<NavigationAttribute> {
  initializeModifier(instance: NavigationAttribute): void {
    instance.mode(NavigationMode.Stack);
    instance.navBarWidth('100%');
    instance.hideTitleBar(true);
    instance.hideToolBar(true);
  }
}

TwoPage

import { HMRouter, HMRouterMgr } from '@hadss/hmrouter'
import { PageModel } from '../../Models/PageModel'

@HMRouter({ pageUrl: "TwoPage" })
@Component
export struct TwoPage {
  aboutToAppear(): void {
    let currentParam: PageModel = HMRouterMgr.getCurrentParam() as PageModel;
    if (currentParam == undefined) {
      return;
    }
    console.debug("param", 'name:' + currentParam.Name);
    console.debug("param", 'age:' + currentParam.Age);
  }

  build() {
    Column({ space: 20 }) {
      Button("ThreePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.push({
            navigationId: "mainNavigation",
            pageUrl: "ThreePage"
          })
        })
      Button("ThreeReplacePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.replace({
            navigationId: "mainNavigation",
            pageUrl: "ThreePage"
          })
        })
      Button("HomePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.pop({
            navigationId: "mainNavigation"
          })
        })
    }
    .height("100%")
    .width("100%")
  }
}

ThreePage

import { HMRouter, HMRouterMgr } from '@hadss/hmrouter'

@HMRouter({ pageUrl: "ThreePage" })
@Component
export struct ThreePage {
  build() {
    Column() {
      Button("ThreePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.pop({
            navigationId: "mainNavigation"
          })
        })
    }
    .height("100%")
    .width("100%")
  }
}

实现页面跳转结果如下:

HMRouterPathInfo


路由跳转接口参数类,属性如下:

属性类型简介
navigationIdstring操作页面栈,为空时表示对最近一次操作的navigation进行路由跳转
pageUrlstring需要跳转的目标页面
paramESObject跳转页面携带的参数
interceptorsIHMInterceptor[]自定义拦截器,最高优先级执行
animatorIHMAnimatorboolean
skipAllInterceptorboolean是否跳过所有拦截器执行,但是不会跳过interceptors中的拦截器

获取路由的参数


通过HMRouterMgr.getCurrentParam()方法来获取页面传递的数据。通过push和replace的callback参数来实现页面返回的命令触发。

    static getCurrentParam(): Object | null;

修改上一章代码

TwoPage

import { HMPopInfo, HMRouter, HMRouterMgr } from '@hadss/hmrouter'
import { PageModel } from '../../Models/PageModel'

@HMRouter({ pageUrl: "TwoPage" })
@Component
export struct TwoPage {
  aboutToAppear(): void {
    let currentParam: PageModel = HMRouterMgr.getCurrentParam() as PageModel;
    if (currentParam == undefined) {
      return;
    }
    console.debug("router", 'name:' + currentParam.Name);
    console.debug("router", 'age:' + currentParam.Age);
  }

  build() {
    Column({ space: 20 }) {
      Button("ThreePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.push({
            navigationId: "mainNavigation",
            pageUrl: "ThreePage"
          }, {
            onResult: (popInfo: HMPopInfo) => {
              let popResult: PageModel = popInfo.result as PageModel;
              if (popResult == null || popResult == undefined) {
                return;
              }
              console.debug("router", 'name:' + popResult.Name);
              console.debug("router", 'age:' + popResult.Age);
            }
          })
        })
      Button("ThreeReplacePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.replace({
            navigationId: "mainNavigation",
            pageUrl: "ThreePage"
          })
        })
      Button("HomePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.pop({
            navigationId: "mainNavigation"
          })
        })
    }
    .height("100%")
    .width("100%")
  }
}

ThreePage

import { HMRouter, HMRouterMgr } from '@hadss/hmrouter'
import { PageModel } from '../../Models/PageModel'

@HMRouter({ pageUrl: "ThreePage" })
@Component
export struct ThreePage {
  build() {
    Column() {
      Button("ThreePage")
        .width("80%")
        .onClick(() => {
          HMRouterMgr.pop({
            navigationId: "mainNavigation",
            param: new PageModel("李四", "18")
          })
        })
    }
    .height("100%")
    .width("100%")
  }
}

实现以下效果

把传递的参数打印出来。

push和replace切换路由传递

pop回传

总结

这篇文章主要讲了路由切换相关的内容。同时需要注意,即使是Replace切换到下一个页面,页面返回时也是调用Replace的回调函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baobao熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值