当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Container.resolve方法代码示例

本文整理汇总了TypeScript中container-ioc.Container.resolve方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Container.resolve方法的具体用法?TypeScript Container.resolve怎么用?TypeScript Container.resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在container-ioc.Container的用法示例。


在下文中一共展示了Container.resolve方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: Container

import { Container } from 'container-ioc';

/*
    If a container can't find a value within itself, it will look it up in ascendant containers.
    There a 3 ways to set a parent for a container:
 */

const grandParentContainer = new Container();

// 1.
const parentContainer = grandParentContainer.createChild();

// 2.
const childContainer = new Container();
childContainer.setParent(parentContainer);

// 3.
const deepNestedContainer = new Container({
    parent: childContainer
});

grandParentContainer.register([
    { token: 'IConfig', useValue: {}}
]);

deepNestedContainer.resolve('IConfig');
开发者ID:typesoft,项目名称:container-ioc,代码行数:26,代码来源:hierarchical-containers.ts

示例2: serve

    You can also specify life time individually for each item in a container by specifying lifeTime attribute.
*/
@Injectable()
class ServiceA {}

/* With classes */
container.register([
    {
        token: 'ISerivceA',
        useClass: ServiceA,
        lifeTime: LifeTime.PerRequest
    }
]);

/* With factories */
container.register([
    {
        token: 'IServiceB',
        useFactory: () => {
            return {
                serve(): void {
                    return;
                }
            };
        },
        lifeTime: LifeTime.Persistent
    }
]);

const service1 = container.resolve('IServiceA');
const service2 = container.resolve('IServiceB');
开发者ID:typesoft,项目名称:container-ioc,代码行数:31,代码来源:life-time-control.ts

示例3: Symbol

import { Container, Injectable } from 'container-ioc';

interface IService {
    [key: string]: any;
}

const TSerivice = Symbol('IService');

@Injectable([TSerivice])
class App {
    constructor(private service: IService) {}
}

const container = new Container();

container.register([
    { token: App, useClass: App },
    { token: TSerivice, useValue: {}}
]);

const app = container.resolve(App);
开发者ID:typesoft,项目名称:container-ioc,代码行数:21,代码来源:alternative-syntax.ts

示例4: Container

import { Container } from 'container-ioc';

const container = new Container();

container.register([
    { token: 'IConfig', useValue: {}}
]);

const app = container.resolve('IConfig');
开发者ID:typesoft,项目名称:container-ioc,代码行数:9,代码来源:use-value.ts


注:本文中的container-ioc.Container.resolve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。