@Inject
Inject decorator is set of dependency injection system.
export declare let Inject: (
value: string | Function, isMutable?: boolean
) => (Class: any, key?: string, paramIndex?: any) => any;
@Inject is used by injector in order to know where and what to inject at certain time. @Inject you can use in services, controllers, modules, route rules, filters.
Usage examples:
In services
@Injectable()
class ServiceA {
@Inject(ServiceB)
private myServ: ServiceB;
}
In controllers:
@Controller({
name: 'home'
providers: []
})
class Controller {
// local to controller
@Inject(ServiceB)
private myServ: ServiceB;
@Action('index')
myCustomActionName( @Inject(ServiceC) myServ2: ServiceC ) {
this.myServ.exec(); // access from local object
myServ2.exec(); // access from local function scope;
}
}
In modules:
@Module({
name: 'myModule'
imports: [ModuleB],
providers: [ServiceB]
})
class MyNewModule implements IAfterConstruct {
@Inject(ServiceB)
private myServ: ServiceB;
afterConstruct(): void {
this.myServ.exec();
}
}
In filters:
@Filter(20)
class BFilter implements IFilter {
@Inject(ServiceB)
private serviceB: ServiceB;
before(data: string): string|Buffer|Promise<string|Buffer> {
return "bFilter <- " + this.serviceB.transform(data);
}
after(data: string): string|Buffer|Promise<string|Buffer> {
return "bFilter <- " + data;
}
}