Init Decorator
Since Injex handles circular dependencies for you, one caveat is that you can't access injectable dependencies from module class constructors. Instead, you can use the @init()
decorator to run module initialization upon creation.
Usage
You use the @init()
decorator by decorating a module class method to behave as the initialize method. This method is called by the Injex runtime after all the modules created and instantiated.
Inheritance
You can use multiple @init()
methods when you inherit from a parent class, this way, you can benefit the auto-initialization in multiple classes.
For example:
The initialization method invokes order is from the parent class to the inherited class, just like when you use a super()
constructor.
Async/Await
Notice that you may return a promise from the initialization method if you like; Injex will "await" for it and return its promise from the factory method.
For example: