【问题标题】:Cannot read properties of undefined (reading 'singleton') - Adonisjs无法读取未定义的属性(读取“单例”) - Adonisjs
【发布时间】:2022-01-05 00:27:06
【问题描述】:

tender 注册单例类型服务时出错。

我正在使用 IocContract

应用程序提供者

export default class AppProvider {
  constructor(
    protected app: ApplicationContract,
    protected $container: IocContract
  ) {}

  public register() {
    // Register your own bindings
    this.$container.singleton(
      'Front/RegraService',
      () => new RegraService()
    )
  }

错误

无法读取未定义的属性(读取“单例”)

【问题讨论】:

  • 发送“$container”不会使其在“this”中可用
  • 从 '@adonisjs/fold' 导入 { IocContract }
  • 导出默认类 AppProvider { $container = null;构造函数(受保护的应用程序:ApplicationContract,受保护的 $container:IocContract){ $container = 容器; } public register() { // 注册你自己的绑定 this.$container.singleton( 'Front/RegraService', () => new RegraService() ) }
  • 这是一个构造 - 受保护的 $container: IocContract
  • 认为“$container”需要是类中的一个属性,需要通过构造函数来更新。

标签: javascript typescript adonis.js


【解决方案1】:
export default class AppProvider {
  constructor(protected app: ApplicationContract) {}

  public register() {
    this.app.container.singleton('Front/RegraService', () => new RegraService())
  }
}

【讨论】:

猜你喜欢
  • 2023-03-11
  • 2022-01-15
  • 2021-12-31
  • 2021-12-13
  • 2021-11-28
  • 2021-11-15
  • 2021-11-06
  • 2021-11-20
  • 2021-11-16
相关资源
最近更新 更多