a library to speed up the development of Fivem scripts and frameworks
fivem-ts - Documentation v0.7.5 • Docs
fivem-ts - Documentation v0.7.5 / Server / Inject
Inject(
target
,propertyKey
,parameterIndex
):void
Injects a dependency into a class property or constructor parameter.
This decorator is used to automatically inject dependencies into class properties or constructor parameters by resolving them from the DI container. It works by leveraging TypeScript’s metadata reflection to determine the type of dependency needed and then retrieving it from the global DI container.
Note: To use this decorator effectively, ensure that the emitDecoratorMetadata
option is enabled in your
tsconfig.json
configuration file.
Example usage:
class MyService {
constructor(@Inject private dependency: AnotherService) {}
}
// Ensure that AnotherService is registered with the DI container
container.registerSingleton(AnotherService);
// Resolve an instance of MyService with its dependencies injected
const myService = container.resolve(MyService);
• target: unknown
The prototype of the class or the constructor function where the dependency is injected.
• propertyKey: string
| symbol
The name of the property or method parameter where the dependency is injected.
• parameterIndex: number
The index of the constructor parameter to be injected with a dependency. This is relevant only for constructor parameters.
void
Error if the dependency cannot be resolved. This might occur if:
emitDecoratorMetadata
option is not enabled in tsconfig.json
.