Harmony HMRouter Interceptor 的基本使用

1 创建Interceptor

php 复制代码
@HMInterceptor({interceptorName: 'LoginCheckInterceptor'})
export class LoginCheckInterceptor implements IHMInterceptor {
  handle(info: HMInterceptorInfo): HMInterceptorAction {
    if(info.srcName === 'PayCard') {
      if(!!AppStorage.get('isLogin')) {
        return HMInterceptorAction.DO_NEXT;
      } else {
        info.context.getPromptAction().showToast({message: LoginConstants.LOGIN_TOAST});
        HMRouterMgr.push({
          pageUrl: 'loginPage',
          skipAllInterceptor: true
        });
        return HMInterceptorAction.DO_REJECT;
      }
    } else {
      if(!!AppStorage.get('isLogin')) {
        return HMInterceptorAction.DO_NEXT;
      } else {
        info.context.getPromptAction().showToast({message: LoginConstants.LOGIN_TOAST});
        HMRouterMgr.push({
          pageUrl: 'loginPage',
          param: info.targetName,
          skipAllInterceptor: true
        });
        return HMInterceptorAction.DO_REJECT;
      }
    }
  }
}

这个是官方demo里面的一个登陆拦截Interceptor。跳转某些需要登陆页面的时候,如果没有登陆返回 HMInterceptorAction.DO_REJECT,取消这次登陆。 如果已经登陆返回HMInterceptorAction.DO_NEXT,正常跳转。

2 使用 Interceptor

less 复制代码
@HMRouter({
  pageUrl: PageConstant.PAY_DIALOG_CONTENT,
  dialog: true,
  lifecycle: 'ExitPayDialog',
  interceptors: ['LoginCheckInterceptor']
})
@Component
export struct PayDialogContent {}

在需要拦截的地方的interceptors中添加 比如

js 复制代码
interceptors: ['LoginCheckInterceptor']

LoginCheckInterceptor 是创建Interceptor的时候填写的interceptorName @HMInterceptor({interceptorName: 'LoginCheckInterceptor'})

3 Interceptor的另一种注册方式

js 复制代码
export class JumpInfoInterceptor implements IHMInterceptor {
  handle(info: HMInterceptorInfo): HMInterceptorAction {
    let connectionInfo: string = '';
    if(info.type === HMActionType.PUSH) {
      connectionInfo = 'jump to';
    } else {
      connectionInfo = 'back to';
    }

    console.log(`${info.srcName} ${connectionInfo} ${info.targetName}`);
    return HMInterceptorAction.DO_NEXT;
  }
}

JumpInfoInterceptor也是一个Interceptor,但是他并没有声明interceptorName。他可以通过这种方式注册。

css 复制代码
HMRouterMgr.registerGlobalInterceptor({
  interceptor: new JumpInfoInterceptor(),
  interceptorName: 'JumpInfo',
  priority: 5
});

4 全局 Interceptor

typescript 复制代码
@HMInterceptor({interceptorName: 'LoginStatusInterceptor', global: true})
export class LoginStatusInterceptor implements IHMInterceptor {
  handle(info: HMInterceptorInfo): HMInterceptorAction {
    console.log(`Login status is ${!!AppStorage.get('isLogin') ? 'Y' : 'N'}`);
    return HMInterceptorAction.DO_NEXT;
  }
}

HMInterceptor 只是声明了global = true; 那么所有的页面跳转都会经过这个Interceptor。

相关推荐
北海zx15 分钟前
HarmonyNext:如何在鸿蒙上录屏后进行音视频编码
harmonyos
别说我什么都不会28 分钟前
【仓颉三方库】音视频开发—— ijkplayer-ffi
harmonyos
王二蛋与他的张大花4 小时前
HarmonyOS运动开发:如何监听用户运动步数数据
harmonyos
冯志浩4 小时前
HarmonyOS - 实现 ArkTS 和 web 页面的数据交互
harmonyos·掘金·金石计划
别说我什么都不会4 小时前
【仓颉三方库】音视频开发—— mp3tag4cj
harmonyos
半青年5 小时前
鸿蒙系统应用开发全栈指南
华为·华为云·harmonyos
HvrI16 小时前
【Harmony_Bug】forEach + asyncawait 的异步陷阱
开发语言·华为·bug·harmonyos·鸿蒙
鸿蒙开发工程师—阿辉16 小时前
一键多环境构建——用 Hvigor 玩转 HarmonyOS Next
ubuntu·华为·harmonyos
NapleC16 小时前
HarmonyOS NEXT:多设备的自由流转
华为·harmonyos
鸿蒙布道师21 小时前
鸿蒙NEXT开发正则工具类RegexUtil(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei