本文整理汇总了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');
示例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');
示例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);
示例4: Container
import { Container } from 'container-ioc';
const container = new Container();
container.register([
{ token: 'IConfig', useValue: {}}
]);
const app = container.resolve('IConfig');