Open inaiei opened 6 years ago
Hi @inaiei,
Did you find any solution for this problem?
I have the same problem in vue.js Did you find any solution for this problem?
I had a similar problem... just in case it helps someone.
In my case I had exported the container and decorator from a ioc.ts
file which was itself exported via an index.ts
file.
That worked for basic DI, but didn't work for the lazyInject
decorator as soon as used in two "circular" classes. the import of all exports of the ioc stuff resulted in undefined
.
As soon as I changed to directly import from the ioc.ts
instead of the index.ts
it started working. So instead of use this two files, I just put everything from ioc.ts
directly in the corresponding index.ts
and it still worked.
did not work, as soon consumed by more than one file that depend on each other.
src
ioc
ioc.ts <-exports the container and decorators (but never consumed directly by any other file)
index.ts <-export * from ioc.ts
class1.ts <- import via src/ioc
class2.ts <- import via src/ioc
when class1.ts
and class2.ts
depends on each other, the following import paths do not work:
class1.ts -> src/ioc/index.ts -> src/ioc/ioc.ts
class2.ts -> src/ioc/index.ts -> src/ioc/ioc.ts
works:
src
ioc
index.ts <-exports the container and decorators
class1.ts <- import via src/ioc/ioc.ts
class2.ts <- import via src/ioc/ioc.ts
when class1.ts
and class2.ts
depends on each other, the following import paths do work:
class1.ts -> src/ioc/index.ts
class2.ts -> src/ioc/index.ts
PS: the strange thing is... I used breakpoints to check if class1.ts
or class2.ts
was called before ioc.ts
but this was not the case... so something behaves badly with the module resolution.
I realize this is an old issue, but I ran into this problem recently and found a workaround.
The problem exists because of a circular dependency between a class (in the case above, the Header
class) and the inversify.config'.
inversify.configneeds to import
Headerin order to create the bindings but
Headerneeds access to
lazyInjectwhich is created in
inversify.config`.
The solution is to split your existing inversify.config
into three files.
container.ts
)inversify.config.ts
)getDecorators()
and exports lazyInject
Your class would then import lazyInject
from that third file instead of inversify.config
.
I have the same problem in react.
I am writing a react app and I have a nested @lazyInject that is not working.
I am trying to use inversify to inject my components as well as other classes used by the components itself.
Here is an example of the problem I am having:
I have a header injected with @lazyInject in my App component.
then on the header component I have another lazyInject for the headerInfo object
This is not working, but if I remove either one them and have just one lazyInject then it works fine.
Here is my inversify.config
Here is the error