【问题标题】:Is there possibillity to call constructor inside Container.get() in typedi?是否有可能在 typedi 中调用 Container.get() 内的构造函数?
【发布时间】:2020-05-26 15:58:23
【问题描述】:

我在 node.js 中使用 typedi

我想知道我是否可以做类似的事情

const obj = Container.get(MyObj(creds));

代替

const obj = new MyObj(creds);

由于 jasmine 单元测试,我需要使用它。我需要相同的对象。

我应该如何在单元测试中获取相同的对象?

【问题讨论】:

    标签: node.js unit-testing dependency-injection jasmine


    【解决方案1】:
    @Service()
    class MyObj {
    
        @Inject("creds")
        creds: any;
    
    }
    

    然后你可以这样注入:

    Container.set("creds", creds);
    const obj = Container.get(MyObj);
    console.log(obj.creds);
    

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多