EN
← 错误索引

F_SYSTEM_MISSING_PROVIDER

缺少注入 Provider

当前 Module 看不到构造函数、Handler 或 factory 请求的 Provider token。

更新于

复现

构造函数请求了带 @Injectable() 的 class,但 owner Module 没有注册或导入对应 Provider:

src/app.module.ts
import { Command, Handler, Injectable, Module, createApp } from 'func'

@Injectable()
class RegistryClient {}

@Command('publish')
class PublishCommand {
  constructor(private registry: RegistryClient) {}

  @Handler()
  run() {}
}

@Module({ commands: [PublishCommand] })
class AppModule {}

createApp(AppModule)

这个错误是什么

运行时已经确定依赖 token,但从请求方 Module 看不到对应 Provider。details.token 是缺失 token,details.requester 是请求 Module;嵌套依赖还会提供 dependencyPath

Module 内的 Provider 默认私有。一个 imported Module 只有把 token 放入 exports,导入方才能解析它;中间 Module 还需要再次 export 才能继续传递。当前命令未激活的 feature option Provider 也不会作为普通依赖开放。

如何修复

在请求方 Module 注册 Provider:

src/app.module.ts
@Module({
  commands: [PublishCommand],
  providers: [RegistryClient],
})
class AppModule {}

如果 Provider 位于另一个 Module,在声明方同时添加 providers: [RegistryClient]exports: [RegistryClient],再由请求方 import 该 Module。接口或非 class 依赖使用 createToken()@Inject(token),并注册相同 token。