You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 13.1+Ionic5中ExerciseService注入报错求助

Angular 13.1 + Ionic 5 服务注入错误排查与修复

一、核心问题根源

1. "NullInjectorError: No provider for X!" 报错原因

你的ExerciseService存在重复注册提供者的矛盾配置:

  • 服务类上已经通过@Injectable({ providedIn: "root" })声明为根注入器提供的单例服务,Angular会自动完成全局注册。
  • 同时又在app.module.ts的providers数组中手动添加了ExerciseService,这会导致依赖注入系统出现冲突,无法正确解析服务实例。

2. "JIT compiler unavailable" 报错原因

将ExerciseService导入tutorial.module.ts时,大概率是误将服务放置到了模块的declarations数组中(该数组仅允许存放组件、指令、管道),触发了Angular的JIT编译路径,但Angular 13默认启用AOT编译,运行时未包含JIT编译器,因此抛出该错误。

二、修复步骤

  • 移除重复注册:删除app.module.ts的providers数组中的ExerciseService条目,仅保留服务类上的providedIn: "root"配置。
  • 清理模块错误配置:检查tutorial.module.ts,确保ExerciseService没有被添加到declarations/entryComponents数组中,组件中仅需正常导入并注入服务即可,无需在模块中额外处理。
  • 重启并清除缓存:执行命令ionic serve --reset-cache清除构建缓存后重启开发服务器,确保Angular重新生成正确的依赖注入树。

内容的提问来源于stack exchange,提问作者MrMik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 02:25:16