當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Container.Container類代碼示例

本文整理匯總了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);
    });
開發者ID:joppe,項目名稱:lab,代碼行數:27,代碼來源:Inject.spec.ts

示例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);
    });
開發者ID:joppe,項目名稱:lab,代碼行數:25,代碼來源:Container.spec.ts

示例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;
}
開發者ID:joppe,項目名稱:lab,代碼行數:18,代碼來源:inject.ts


注:本文中的@lib/dependency-injection/Container.Container類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。