Closed jekabs-karklins closed 3 years ago
You shouldn't use both @singleton
and @injectable
at the same time - I suspect that might be the cause of your issue.
You shouldn't use both
@singleton
and@injectable
at the same time - I suspect that might be the cause of your issue.
Hi, I have updated the example. The result is still the same
Ah, I think I have it - @singleton
registers the type in the global container using the type as the injection token. Your call to container.register("Foo", { useClass: Foo });
registers the type using the string "Foo"
as the token. So a couple of possibilities here
Foo
registration, and remove the @inject("Foo")
from the Bar
ctor, orFoo
to use @injectable
, and register it using the singleton lifecycle like: container.register("Foo", {useClass: Foo}, { lifecycle:Lifecycle.Singleton});
Thank you, that was it. Registering Foo like this solves the issue
container.register(
"Foo",
{ useClass: Foo },
{ lifecycle: Lifecycle.Singleton }
);
Describe the bug Class decorated with @singleton is created multiple times.
To Reproduce
Expected behavior Console output
Actual behavior Console output
Version: 4.4.0