【OpenHarmony】鸿蒙开发之Arouter

100 阅读4分钟

简介

Arouter是一款轻量且高效的适用于OpenHarmony的页面路由工具。

  • 支持页面间路由跳转;
  • 支持带参数跳转及回调;
  • 支持配置跳转拦截器;
  • 支持预处理跳转与否;
  • 支持跨模块路由跳转。

效果图

下载安装

ohpm install @ohos/arouteronactivityresult

使用说明

路由跳转

1.不传参跳转

通过Arouter.getInstance()创建路由对象,使用链式调用方法 build('')配置跳转的页面,navigation() 方法进行页面跳转。

import {Arouter} from "@ohos/arouteronactivityresult";
Arouter.getInstance()
    .build("--/--")  //需要跳转的地址
    .navigation()

2.传参跳转

在不传参跳转的基础上,跳转之前通过withParams()进行参数配置。

import {Arouter} from "@ohos/arouteronactivityresult";
Arouter.getInstance()
    .build("--/--")  //需要跳转的地址
    .withParams({index:"--"})
    .navigation()

3.路由回调

路由回调需要配合NavigationCallback接口进行,在路由前的页面实现NavigationCallback接口

import {NavigationCallback} from '@ohos/arouteronactivityresult'
var callback:NavigationCallback = {
    onInterrupt(postcard){},
    onArrival(postcard){},
    onActivityResult(data){}
}

然后将callback传入 .navigationWithCallback()中进行跳转

import {Arouter} from "@ohos/arouteronactivityresult";
Arouter.getInstance()
    .build("--")//需要跳转的地址
    .navigationWithCallback(callback)

在目标页面的onPageShow()生命周期中调用getPostcard()方法获取到指定的postcard

import router from '@ohos.router';
if (postcard == null) {
    postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name);
}

使用 postcard.getNavigationCallback() 方法调用对应的回调方法,即可回调源页面实现的方法

postcard.getNavigationCallback().onActivityResult(params)

路由拦截

1.配置拦截器

在拦截器中的process()方法中实现页面的拦截,通过interceptorCallback.onInterrupt()中断跳转,interceptorCallback.onContinue()继续跳转。

import {Postcard,IInterceptor,InterceptorCallback} from '@ohos/arouteronactivityresult';
var iInterceptor:IInterceptor= {
    process(postcard:Postcard, interceptorCallback:InterceptorCallback) {
        // 选择拦截的页面,若跳转时有该路径则进行拦截提示,若没有则直接跳转
        if (Postcard.getUri() == 'pages/transit') {
            // 选择弹框
            AlertDialog.show(
                {
                    message: '被拦截了,点击继续跳转',
                    primaryButton: {
                        value: '取消',
                        action: () => {
                            // 中断跳转
                            interceptorCallback.onInterrupt(postcard)
                        }
                    },
                    secondaryButton: {
                        value: '继续',
                        action: () => {
                            // 继续跳转
                            interceptorCallback.onContinue(postcard);
                        }
                    },
                }
            )
        } else {
            // 继续跳转
            interceptorCallback.onContinue(postcard);
        }
    }
}

2.注册拦截器

import {registerInterceptor} from '@ohos/arouteronactivityresult';
registerInterceptor(iInterceptor);

3.移除拦截器

import {unregisterInterceptor} from '@ohos/arouteronactivityresult';
unregisterInterceptor()

4.配置绿色通道

在跳转前使用.setGreenChannel()方法跳过拦截(true:跳过拦截)。

Arouter.getInstance()
    .build("--/--")//需要跳转的地址
    .setGreenChannel(true)
    .navigation()

DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。

`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点学习。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

5.配置预处理跳转与否

预处理:实现 PretreatmentService 接口中 onPretreatment 方法,并返回一个Boolean值(true:继续跳转,false:不跳转)。

import {PretreatmentService} from '@ohos/arouteronactivityresult';
var pretreatmentService:PretreatmentService = {
  onPretreatment(postcard:Postcard):boolean{
    return true
  }
}

在跳转前调用.setPretreatmentService() 方法,将 pretreatmentService传入 setPretreatmentService()方法中完成预处理功能。

Arouter.getInstance()
    .build(this.router)
    .setPretreatmentService(pretreatmentService)
    .navigationWithCallback(callback)

6.跨模块路由跳转 api10+

  1. 需要在当前应用包的oh-package.json5文件中配置依赖。例如:
"dependencies": {
"@ohos/libraryOne": "file:../libraryOne",
...
}

2. 在跳转的目标页面内,给@Entry修饰的自定义组件EntryOptions命名

@Entry({ routeName: 'myPageOne' })
@Component
struct Index {
...
}

3. 在触发跳转的页面内引入目标页面,并使用Arouter

import '@ohos/library/src/main/ets/pages/Index';
...
Arouter.getInstance()
      .build("myPageOne",true)
      .setGreenChannel(true)
      .navigation()
...      

接口说明

Arouter

方法名入参接口描述
build(string,boolean)配置页面跳转路径(必填),是否跨页面跳转(非必填默认为false)
withParams{ }传入另一页面的参数
navigation正常跳转
navigationWithCallbackNavigationCallback带回调跳转
setGreenChannelboolean配置是否为绿色通道
registerInterceptoriInterceptor注册拦截器
unregisterInterceptor移除拦截器
getNavigationCallback获取状态回调方法
setUristring设置页面跳转路径
getUri获取跳转的页面路径
getParams获取跳转传递的参数
getTag获取标签
setTag{ }设置标签
withFlagsboolean设置flags
getFlagsboolean获得flags
toString导出字符串
setPretreatmentServicePretreatmentService预处理
getPostcard页面路径找到指定的postcard

回调接口

接口名入参接口描述
NavigationCallback.onArrivalPostcard到达回调地
NavigationCallback.onInterruptPostcard回调中断
NavigationCallback.onActivityResultany回调结果
IInterceptor.processPostcard,InterceptorCallback拦截过程
InterceptorCallback.onContinuePostcard拦截器回调继续
InterceptorCallback.onInterruptPostcard拦截器回调暂停
PretreatmentService.onPretreatmentPostcard预处理实现

目录结构

|---- arouteronactivityresult  
|     |---- src/main/ets/components/arouter # 路由跳转实现逻辑
|     |---- index.ets  # 对外接口