ttdi
v1.0.6
Published
![GitHub](https://img.shields.io/github/license/Luncher/ttdi?style=for-the-badge) ![npm](https://img.shields.io/npm/v/ttdi?style=for-the-badge) ![Travis (.com)](https://img.shields.io/travis/com/Luncher/ttdi?style=for-the-badge)
Downloads
2
Readme
ttdi
ttdi
is a dependency injection tool for TypeScript and JavaScript.
features:
- property based injection
- constructor based injection
- support for multiple DI containers
- auto collection providers(injectable)
Quick Start
Prerequisite
ttdi leverage decorator and metadata, you need to enable emitting decorator metadata in your Typescript config.Add these two lines to your tsconfig.json file under the compilerOptions key:
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
For the decorator of TypeScript, please refer to decorators
Install
yarn add ttdi -S
or
npm i ttdi -S
Usage
declare.ts
import { Inject, Injectable } from 'ttdi'
@Injectable()
export class Bar {
constructor() {
}
}
@Injectable()
export class Baz {
}
@Injectable()
export class Foo {
@Inject
baz!: Baz
constructor(public bar: Bar) {}
}
main.ts
import { configure, Container } from 'ttdi'
import { Bar, Baz, Foo } from './declare'
const container = new Container()
await configure(container, __dirname + '/declare.ts')
const foo = container.get(Foo)
expect(foo).toBeInstanceOf(Foo)
expect(foo.bar).toBeInstanceOf(Bar)
expect(foo.baz).toBeInstanceOf(Baz)
API
@Injectable()
The tag class can be injected as a provider.
@Inject()
class property inject.
notice: constructor argument will auto injected.
configure
a async providers(injectable) loader.
container
instance storage container, you can create multi DI containers.