【问题标题】:Injecting a service in nestjs在nestjs中注入服务
【发布时间】:2020-03-11 14:55:46
【问题描述】:

我正在重构一些遗留代码,并且我编写了一个服务,我希望将其注入到类的构造函数中。关于这门课,有两件重要的事情需要注意。 第一件事是它是由工厂实例化的,第二件事是它是另一个类的父级。

class A extends AParent implements IA {
    constructor(
        protected someService: SomeService,
    )
}


class B extends A {
    constructor(
        protected someService: SomeService,
    )
    super(someService);
}

现在工厂说它不能实例化 A 类,因为它的构造函数需要一个参数,即服务。

解决此问题的正确方法是什么?

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    不知道,这可能是一个答案,但无论如何。

    class A extends AParent implements IA {
      constructor(protected someService: SomeService) // Removing `,` after service argument
      {
        // Forgotten `{}` brackets.
      }
    }
    
    class B extends A {
      constructor()
      {
        // Since you already have an instance of `SomeService` in parent class
        // I think you don't need to pass it to constructor of class B.
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2021-12-29
      • 1970-01-01
      • 2020-07-08
      • 2019-02-26
      • 1970-01-01
      • 2022-09-24
      • 2018-11-16
      • 2020-06-23
      相关资源
      最近更新 更多