我使用的是@发射器/注射器 (具有异步初始化支持的微软注射器的一个分支),这非常方便。
我想实现如下内容:
一个编排服务,其构造函数接受依赖服务(可以使用@ injected注入)和一个运行时值:
@injectable()
class OrchestrationService {
constructor(
@inject(ServiceA) private dependencyA: ServiceA,
@inject(ServiceA) private dependencyB: ServiceB,
runtimeValue: string,
) {
this.init(runtimeValue);
}
}然后,我可以使用运行时值来解析实例:
const orchestrationService = container.resolve<OrchestrationService>(runtimeValue);我不知道所有可能的运行时值,因此不能事先注册它们。
我想知道注入运行时值是否可行,以及如何实现这一点。
发布于 2022-04-04 00:02:28
我在tsyringe repo上发布了同样的问题,回答了我的问题。这并不完美,但我认为这是我们所能得到的最好的。
您可以在容器中注册值。 constructor:InjectionToken= "MyToken";类服务{构造函数( @inject(ServiceA)私有dependencyA: ServiceA,@inject(ServiceA)私有dependencyB: ServiceB,@inject(SomeToken) runtimeValue: string,)} container.registerInstance(SomeToken," runtimeValue ");container.resolve(SomeToken);// runtimeValue container.resolve(服务);//服务实例 https://github.com/microsoft/tsyringe#injecting-primitive-values-named-injection
https://github.com/microsoft/tsyringe/issues/193#issuecomment-1084181366
https://stackoverflow.com/questions/71659704
复制相似问题