Dependency injection library for JavaScript and TypeScript. It is an extraction of the Angular's dependency injection which means that it's feature complete, fast, reliable and well tested. Also - retrieve all API documentation with example code.
Up-to-date with Angular 4.4.7, but with support DI to inherited class (when the child class does not have a constructor, while the parent class has it), and with exported makeDecorator(), makeParamDecorator(), makePropDecorator(), makePropTypeDecorator(), reflector, Type (see reflector test).
npm i @ts-stack/diAlso you need to install reflect-metadata module:
npm i reflect-metadataThen, in tsconfig.json file, for compilerOptions you need to set experimentalDecorators and emitDecoratorMetadata to true:
{
"compilerOptions": {
// ...
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}import 'reflect-metadata';
import { ReflectiveInjector, Injectable } from '@ts-stack/di';
@Injectable()
class UsefulService {
}
@Injectable()
class NeedsService {
constructor(public service: UsefulService) {}
}
const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]);
const needsService = injector.get(NeedsService);
expect(needsService instanceof NeedsService).toBe(true);
expect(needsService.service instanceof UsefulService).toBe(true);For more examples, see the tests for @ts-stack/di.
For full documentation check Angular DI docs: