Function: Inject()
Call Signature
Inject<
ClassPrototype>():PropertyDecorator<ClassPrototype>
Inject into the property the object registered in the container under the property's name. The injection happens when the object is created, before its @Configurable properties are set and before its init() runs; it replaces a @Configurable declaration of the property in a parent class.
Type Parameters
ClassPrototype
ClassPrototype extends BaseObject
Returns
PropertyDecorator<ClassPrototype>
The property decorator.
Throws
When the object is created, if the registration cannot be resolved (the creation of the object rejects).
Example
import {Application, Component} from 'lakutata'
import {Inject} from 'lakutata/decorator/di'
class Mailer extends Component {
public async send(to: string, text: string): Promise<void> {
console.log(to, text)
}
}
class Signup extends Component {
//The component registered as "mailer"
@Inject()
protected readonly mailer!: Mailer
//The object registered for the class (a class marked with @Autoload is registered on first use)
@Inject(Application)
protected readonly app!: Application
//The component registered as "mailer", transformed: its send function bound to it
@Inject('mailer', (mailer: Mailer) => mailer.send.bind(mailer))
protected readonly send!: (to: string, text: string) => Promise<void>
public async register(email: string): Promise<void> {
await this.mailer.send(email, `Welcome to ${this.app.appName}`)
}
}
Application.run(() => ({
id: 'signup.app',
name: 'Signup',
components: {
mailer: {class: Mailer},
signup: {class: Signup}
}
}))Call Signature
Inject<
ClassPrototype>(name):PropertyDecorator<ClassPrototype>
Inject into the property the object registered in the container under a name, or for a class. The injection happens when the object is created, before its @Configurable properties are set and before its init() runs; it replaces a @Configurable declaration of the property in a parent class.
Type Parameters
ClassPrototype
ClassPrototype extends BaseObject
Parameters
name
InjectionName
The registration name (a string or a symbol), or a class: the object registered for the class, which is registered on first use when the class is marked with @Autoload().
Returns
PropertyDecorator<ClassPrototype>
The property decorator.
Throws
When the object is created, if the registration cannot be resolved (the creation of the object rejects).
Call Signature
Inject<
ClassPrototype>(transform):PropertyDecorator<ClassPrototype>
Inject into the property a value derived from the object registered in the container under the property's name. The injection happens when the object is created, before its @Configurable properties are set and before its init() runs; it replaces a @Configurable declaration of the property in a parent class.
Type Parameters
ClassPrototype
ClassPrototype extends BaseObject
Parameters
transform
InjectionTransformFunction
A function (not a class) receiving the registered object and returning the value set to the property; it may be async, the property then receives the value it resolves to.
Returns
PropertyDecorator<ClassPrototype>
The property decorator.
Throws
When the object is created, if the registration cannot be resolved (the creation of the object rejects); an error of transform rejects the creation as it is.
Call Signature
Inject<
ClassPrototype>(name,transform):PropertyDecorator<ClassPrototype>
Inject into the property a value derived from the object registered in the container under a name, or for a class. The injection happens when the object is created, before its @Configurable properties are set and before its init() runs; it replaces a @Configurable declaration of the property in a parent class.
Type Parameters
ClassPrototype
ClassPrototype extends BaseObject
Parameters
name
InjectionName
The registration name (a string or a symbol), or a class: the object registered for the class, which is registered on first use when the class is marked with @Autoload().
transform
InjectionTransformFunction
A function receiving the registered object and returning the value set to the property; it may be async, the property then receives the value it resolves to.
Returns
PropertyDecorator<ClassPrototype>
The property decorator.
Throws
When the object is created, if the registration cannot be resolved (the creation of the object rejects); an error of transform rejects the creation as it is.