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。

相关推荐
弓.长.1 分钟前
小白基础入门 React Native 鸿蒙跨平台开发:GestureResponder滑动删除
react native·react.js·harmonyos
弓.长.2 分钟前
小白基础入门 React Native 鸿蒙跨平台开发:多种文本装饰
react native·react.js·harmonyos
前端不太难5 分钟前
HarmonyOS 应用模型,对游戏架构意味着什么
游戏·架构·harmonyos
小风呼呼吹儿2 小时前
Flutter 框架跨平台鸿蒙开发 - 社区团购记账应用开发教程
flutter·华为·harmonyos
2501_944424129 小时前
Flutter for OpenHarmony游戏集合App实战之贪吃蛇食物生成
android·开发语言·flutter·游戏·harmonyos
不会写代码00010 小时前
Flutter 框架跨平台鸿蒙开发 - 全国景区门票查询应用开发教程
flutter·华为·harmonyos
猛扇赵四那边好嘴.11 小时前
Flutter 框架跨平台鸿蒙开发 - 旅行规划助手应用开发教程
flutter·华为·harmonyos
紫雾凌寒12 小时前
【 HarmonyOS 面试题】2026 最新 ArkTS 语言基础面试题
华为·面试·程序员·华为云·职场发展·harmonyos·arkts
摘星编程13 小时前
React Native鸿蒙:BiometricAuth指纹解锁实现
react native·react.js·harmonyos
2501_9444241214 小时前
Flutter for OpenHarmony游戏集合App实战之俄罗斯方块七种形状
android·开发语言·flutter·游戏·harmonyos