本文整理汇总了TypeScript中@lib/dependency-injection/Container.Container类的典型用法代码示例。如果您正苦于以下问题:TypeScript Container类的具体用法?TypeScript Container怎么用?TypeScript Container使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Container类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('resolve value from di container', (): void => {
const di: DIContainer = factory();
di.register<number>(
'a',
() => {
return 16;
}
);
@inject
class Adder {
private readonly _a: number;
constructor(a: number) {
this._a = a;
}
public add(b: number): number {
return this._a + b;
}
}
const adder: Adder = di.resolve<Adder>('Adder');
expect(adder.add(3)).toBe(19);
});
示例2: it
it('resolve a dependency that has no other dependencies', (): void => {
const container: DIContainer = new DIContainer();
container.register<number>(
'a',
() => {
return 12;
}
);
container.register<number>(
'b',
() => {
return 45;
}
);
container.register<number>(
'add',
// tslint:disable-next-line typedef
(a, b) => {
return a + b;
}
);
expect(container.resolve('add')).toEqual(57);
});
示例3: factory
export function inject<T>(constructor: Constructor<T>): Constructor<T> {
const di: Container = factory();
const CONSTRUCTOR_NAME: string = 'name';
const identifier: string = constructor[CONSTRUCTOR_NAME];
const requiredDependencies: Array<string> = args(constructor);
di.register(
identifier,
// tslint:disable-next-line no-any
(dependencies: Array<any>): T => {
return new constructor(...dependencies);
},
requiredDependencies
);
return constructor;
}