我有一个现有的代码通过私有构造函数和返回对象实例来实现单例模式 -
export class SingletonFactory {
private static factoryInstance = new SingletonFactory();
private constructor() {
}
public static getInstance() {
return SingletonFactory.factoryInstance;
}
}
我需要向这个工厂注入一个依赖项。我将我的代码更改为以下 -
@Inject('MyService')
export class SingletonFactory {
private static factoryInstance = new SingletonFactory();
private constructor(private myService : MyService) {
}
public static getInstance() {
return SingletonFactory.factoryInstance;
}
}
请建议,我如何在构造函数中的对象创建时注入依赖项?