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。

相关推荐
MarkHD3 小时前
第十八天 WebView深度优化指南
华为·harmonyos
塞尔维亚大汉4 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【MIPI CSI】
harmonyos·领域驱动设计
别说我什么都不会4 小时前
鸿蒙轻内核M核源码分析系列十五 CPU使用率CPUP
操作系统·harmonyos
塞尔维亚大汉6 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【I3C】
harmonyos·领域驱动设计
今阳8 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
余多多_zZ8 小时前
鸿蒙初学者学习手册(HarmonyOSNext_API14)_组件截图(@ohos.arkui.componentSnapshot (组件截图) )
学习·华为·harmonyos·鸿蒙·鸿蒙系统
一个处女座的程序猿O(∩_∩)O10 小时前
鸿蒙开发技术指南:从入门到精通
华为·harmonyos
陈无左耳、12 小时前
HarmonyOS学习第4天: DevEco Studio初体验
学习·华为·harmonyos
月未央14 小时前
HarmonyOS Next 开发系列:Local 状态管理实践
harmonyos
openinstall全渠道统计1 天前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos