Injector is a inversion of control (IoC) container for TypeScript and JavaScript applications.
Injector 是一个用于 TypeScript 和 JavaScript 应用程序的控制反转 (IoC) 容器。
import { ValueProvider, injectable, inject, injector } from '@unicreators/injector';
@injectable()
class B { }
@injectable()
class A { constructor(public b: B, @inject('value1') v1: number) { } }
injector.register(new ValueProvider('value1', 10));
let a = injector.get<A>(A);
let b = injector.get<B>(B);
expect(a).instanceOf(A);
expect(a.b).equals(b);
expect(a.v1).equals(10);
🍉 Example
$ npm install @unicreators/injector reflect-metadata
使用 injector 时 TypeScript 版本需 >= 4,并且需配置
tsconfig.json
开启experimentalDecorators
和emitDecoratorMetadata
选项。
{
"compilerOptions": {
"target": "es5",
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}