【鸿蒙Harmony】HMRouter问题记录

0 阅读1分钟

1. [HMRouter ERROR]ERR_DYNAMIC_IMPORT_FAILED 40005008 Dynamic import failed - entry/src/main/ets/lifecycle/XXXLifecycle - com.example.hmrouter_sample/entry - XXXLifecycle

示例代码

// ExitAppLifecycle.ets
@HMLifecycle({lifecycleName: 'ExitAppLifecycle'})
export default class ExitAppLifecycle implements IHMLifecycle {
  private lastTime: number = 0;
  onBackPressed(ctx: HMLifecycleContext): boolean {
    let time = new Date().getTime();
    if(time - this.lastTime > 1000) {
      this.lastTime = time;
      ctx.uiContext.getPromptAction().showToast({
        message: CommonConstants.EXIT_TOAST,
        duration: 1000
      });
      return true;
    } else {
      return false;
    }
  }
}
@HMRouter({ pageUrl: "Test", singleton: true, lifecycle: "ExitAppLifecycle" })
@Component
export struct TestPage {
  build() {
    Column() {
      Text("Hello World")
    }
  }
}

重点报错日志

[HMRouter DEBUG][LoadModule] load module with info success ,module path is entry/src/main/ets/lifecycle/ExitAppLifecycle,module info is com.example.hmrouter_sample/entry
[HMRouter ERROR]ERR_DYNAMIC_IMPORT_FAILED 40005008 Dynamic import failed - entry/src/main/ets/lifecycle/ExitAppLifecycle - com.example.hmrouter_sample/entry - ExitAppLifecycle

问题描述

当使用HMRouter Lifecycle 生命周期时报错。局部生命周期使用时,加载成功但无法动态引入。

问题说明

这是因为声明IHMLifecycle的类中,export default class ExitAppLifecycle中的default关键字导致的,添加关键字后,在TestPage.ets文件中@HMRouter装饰器中,lifecycle: 'ExitAppLifecycle',不能找到对应的类,HMRouter库发生了错误,

如何解决

删除 default关键字即可