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。

相关推荐
南村群童欺我老无力.27 分钟前
鸿蒙 - TextInput高度设置的边界行为
华为·harmonyos
木斯佳1 小时前
HarmonyOS 悬浮球实战:从页面内组件到 SubWindow 方案
harmonyos·悬浮球
Lanren的编程日记3 小时前
Flutter 鸿蒙应用AR功能集成实战:多平台AR框架+模拟模式,打造增强现实体验
flutter·ar·harmonyos
南村群童欺我老无力.3 小时前
鸿蒙PC开发的Slider组件blockSize参数的类型要求
华为·harmonyos
前端技术4 小时前
华为余承东:鸿蒙终端设备数突破5500万
java·前端·javascript·人工智能·python·华为·harmonyos
代码论斤卖4 小时前
OpenHarmony的watchdog service频繁崩溃问题分析
linux·harmonyos
Lanren的编程日记4 小时前
Flutter 鸿蒙应用权限管理功能实战:标准化权限申请与状态管控,提升用户信任度
flutter·华为·harmonyos
想你依然心痛4 小时前
HarmonyOS 6(API 23)实战:基于 HDS 沉浸光感与悬浮导航打造“光影工作台“多窗口协作系统
microsoft·华为·harmonyos·悬浮导航·沉浸光感
Ww.xh4 小时前
OpenHarmony API 9 升级到 API 10 权限与接口变更实战指南
服务器·华为·harmonyos
枫叶丹45 小时前
【HarmonyOS 6.0】ArkWeb新特性:PDF加载成功/失败回调及滚动到底部监听
华为·pdf·harmonyos