Inject Decorator
The main task of a dependency-injection framework is to inject modules as dependencies into other modules so that you could keep your code organized and reusable.
Usage
Use the @inject()
decorator to inject your pre-defined modules into others by decorating class properties that share the same name with a module.
Inheritance
Injectables are also inherited when you extend classes or abstractions, so if you have a class with injected dependencies, they are accessible from the inherited class.
For example, notice how we don't decorate the abstract Animal
class with the @define()
decorator since we don't instantiate it directly, but we can still use @inject()
.
The Dog
class, which extends the abstract Animal
class, accesses its parent's logger
dependency.
Custom property names
Class property names should match the module names you inject so Injex could find them. It's not always the case; sometimes, you want to inject a module with a different name.
The @inject()
decorator optionally accepts a module name or a type so that you can use a different name for your class properties.
Factory vs. Singleton
As you may already saw, you can create modules as singletons with the @singleton()
decorator. When you don't use a singleton for a module, it's injectable become a factory method you can call to create an instance.
The factory method calls the constructor, so it accepts arguments.