容器 (Container)
InversifyJS 容器是首先通过绑定配置依赖项的地方,可能稍后会重新配置和删除。在这方面可以直接处理容器,也可以使用容器模块。
您可以使用 get 方法查询配置并解析配置的依赖项。
您可以使用容器激活处理程序对解析做出反应,并使用容器停用处理程序对解绑做出反应。
您可以创建容器层次结构,其中容器祖先可以为后代提供依赖项。
为了进行测试,可以将状态作为快照保存在堆栈上,并在以后恢复。
为了进行高级控制,您可以应用中间件来拦截解析请求和解析的依赖项。
您甚至可以提供自己的注释解决方案。
容器选项
可以将容器选项传递给 Container 构造函数,如果您不提供或提供但省略了选项,则将提供默认值。 构建后可以更改选项,如果您不为从 Container 创建的子容器提供选项,则子容器将共享这些选项。
defaultScope
当绑定到 to/toSelf/toDynamicValue/toService 时,默认作用域为 transient。
其他绑定只能绑定在 singleton 作用域中。
您可以使用容器选项在应用程序级别更改默认为 transient 的绑定的默认作用域:
const container: Container = new Container({ defaultScope: 'Singleton' });
// You can configure the scope when declaring bindings:
container.bind<Warrior>(warriorServiceId).to(Ninja).inTransientScope();
autoBindInjectable
您可以使用此选项激活 @injectable() 装饰类的自动绑定。
每当通过 get 请求实例时,如果未找到请求服务的绑定,容器将尝试添加绑定。
const container: Container = new Container({ autoBindInjectable: true });
// returns false
container.isBound(Ninja);
// returns a Ninja
container.get(Ninja);
// returns true
container.isBound(Ninja);
手动定义的绑定将优先:
const container: Container = new Container({ autoBindInjectable: true });
// returns a Ninja
container.bind(Ninja).to(NinjaMaster);
// returns NinjaMaster
container.get(Ninja);
skipBaseClassChecks
您可以使用此选项跳过检查基类的 @injectable 属性,如果您的任何 @injectable 类扩展了您无法控制的类(第三方类),这尤其有用。默认情况下,此值为 false。
const container = new Container({ skipBaseClassChecks: true });
Container.merge
Container.merge(a: interfaces.Container, b: interfaces.Container, ...containers: interfaces.Container[]): interfaces.Container;
创建一个包含两个或多个容器的绑定(克隆绑定)的新容器:
@injectable()
class Ninja {
public readonly name: string = 'Ninja';
}
@injectable()
class Shuriken {
public readonly name: string = 'Shuriken';
}
const NINJA_EXPANSION_TYPES = {
Ninja: 'Ninja',
Shuriken: 'Shuriken',
} satisfies Record<string, string>;
const ninjaExpansionContainer: Container = new Container();
ninjaExpansionContainer.bind<Ninja>(NINJA_EXPANSION_TYPES.Ninja).to(Ninja);
ninjaExpansionContainer
.bind<Shuriken>(NINJA_EXPANSION_TYPES.Shuriken)
.to(Shuriken);
@injectable()
class Samurai {
public readonly name: string = 'Samurai';
}
@injectable()
class Katana {
public name = 'Katana';
}
const SAMURAI_EXPANSION_TYPES = {
Katana: 'Katana',
Samurai: 'Samurai',
} satisfies Record<string, string>;
const samuraiExpansionContainer: Container = new Container();
samuraiExpansionContainer
.bind<Samurai>(SAMURAI_EXPANSION_TYPES.Samurai)
.to(Samurai);
samuraiExpansionContainer
.bind<Katana>(SAMURAI_EXPANSION_TYPES.Katana)
.to(Katana);
const gameContainer: interfaces.Container = Container.merge(
ninjaExpansionContainer,
samuraiExpansionContainer,
);
applyCustomMetadataReader
applyCustomMetadataReader(metadataReader: interfaces.MetadataReader): void;
设置自定义元数据读取器。请参阅 中间件。
applyMiddleware
applyMiddleware(...middleware: interfaces.Middleware[]): void;
可用于横切关注点的高级功能。请参阅 中间件。
bind
bind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>
设置新绑定。
除非另有说明,否则绑定作用域默认为 transient。
createChild
createChild(containerOptions?: interfaces.ContainerOptions): Container;
创建 容器层次结构。默认提供父 ContainerOptions。
get
get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T;
通过其运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,并且绑定必须同步解析,否则将抛出错误。
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana);
const katana: Weapon = container.get<Weapon>('Weapon');
getAsync
getAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): Promise<T>;
通过其运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,否则将抛出错误。
async function buildLevel1(): Promise<Level1> {
return new Level1();
}
const container: Container = new Container();
container
.bind('Level1')
.toDynamicValue(async (): Promise<Level1> => buildLevel1());
const level1: Promise<Level1> = container.getAsync<Level1>('Level1');
getNamed
getNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T;
通过与其给定的命名约束匹配的运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,并且绑定必须同步解析,否则将抛出错误:
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).whenTargetNamed('melee');
container.bind<Weapon>('Weapon').to(Shuriken).whenTargetNamed('ranged');
const katana: Weapon = container.getNamed<Weapon>('Weapon', 'melee');
const shuriken: Weapon = container.getNamed<Weapon>('Weapon', 'ranged');
getNamedAsync
getNamedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): Promise<T>;
通过与其给定的命名约束匹配的运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,否则将抛出错误:
const container: Container = new Container();
container
.bind<Weapon>('Weapon')
.toDynamicValue(async () => new Katana())
.whenTargetNamed('melee');
container
.bind<Weapon>('Weapon')
.toDynamicValue(async () => new Shuriken())
.whenTargetNamed('ranged');
const katana: Promise<Weapon> = container.getNamedAsync<Weapon>(
'Weapon',
'melee',
);
const shuriken: Promise<Weapon> = container.getNamedAsync<Weapon>(
'Weapon',
'ranged',
);
getTagged
getTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T;
通过与其给定的标记约束匹配的运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,并且绑定必须同步解析,否则将抛出错误:
const container: Container = new Container();
container
.bind<Weapon>('Weapon')
.to(Katana)
.whenTargetTagged('faction', 'samurai');
container
.bind<Weapon>('Weapon')
.to(Shuriken)
.whenTargetTagged('faction', 'ninja');
const katana: Weapon = container.getTagged<Weapon>(
'Weapon',
'faction',
'samurai',
);
const shuriken: Weapon = container.getTagged<Weapon>(
'Weapon',
'faction',
'ninja',
);
getTaggedAsync
getTaggedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T>;
通过与其给定的标记约束匹配的运行时标识符解析依赖项。运行时标识符必须仅与一个绑定关联,否则将抛出错误:
const container: Container = new Container();
container
.bind<Weapon>('Weapon')
.toDynamicValue(async () => new Katana())
.whenTargetTagged('faction', 'samurai');
container
.bind<Weapon>('Weapon')
.toDynamicValue(async () => new Shuriken())
.whenTargetTagged('faction', 'ninja');
const katana: Promise<Weapon> = container.getTaggedAsync<Weapon>(
'Weapon',
'faction',
'samurai',
);
const shuriken: Promise<Weapon> = container.getTaggedAsync<Weapon>(
'Weapon',
'faction',
'ninja',
);
getAll
getAll(serviceIdentifier: interfaces.ServiceIdentifier<T>, options?: interfaces.GetAllOptions): T[];
获取给定标识符的所有可用绑定。所有绑定必须同步解析,否则将抛出错误:
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana);
container.bind<Weapon>('Weapon').to(Shuriken);
const weapons: Weapon[] = container.getAll<Weapon>('Weapon');
请记住,container.getAll 默认情况下不在根级别强制执行绑定约束,启用 enforceBindingConstraints 标志以强制执行此绑定约束检查:
const container: Container = new Container();
container
.bind<Weapon>('Weapon')
.to(Katana)
.when(() => true);
container
.bind<Weapon>('Weapon')
.to(Shuriken)
.when(() => false);
// returns [new Katana(), new Shuriken()]
const allWeapons: Weapon[] = container.getAll<Weapon>('Weapon');
// returns [new Katana()]
const notAllWeapons: Weapon[] = container.getAll<Weapon>('Weapon', {
enforceBindingConstraints: true,
});
getAllAsync
getAllAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, options?: interfaces.GetAllOptions): Promise<T[]>
获取给定标识符的所有可用绑定:
const container: Container = new Container();
container.bind<Weapon>('Weapon').toDynamicValue(async () => new Katana());
container.bind<Weapon>('Weapon').to(Shuriken);
const weapons: Promise<Weapon[]> = container.getAllAsync<Weapon>('Weapon');
请记住,container.getAll 默认情况下不在根级别强制执行绑定约束,启用 enforceBindingConstraints 标志以强制执行此绑定约束检查:
const container: Container = new Container();
container
.bind<Weapon>('Weapon')
.toDynamicValue(async () => new Katana())
.when(() => true);
container
.bind<Weapon>('Weapon')
.to(Shuriken)
.when(() => false);
// returns [new Katana(), new Shuriken()]
const allWeapons: Promise<Weapon[]> = container.getAllAsync<Weapon>('Weapon');
// returns [new Katana()]
const notAllWeapons: Promise<Weapon[]> = container.getAllAsync<Weapon>(
'Weapon',
{
enforceBindingConstraints: true,
},
);
getAllNamed
getAllNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T[];
通过与其给定的命名约束匹配的运行时标识符解析所有依赖项。所有绑定必须同步解析,否则将抛出错误:
const container: Container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container
.bind<Intl>('Intl')
.toConstantValue({ hello: 'bonjour' })
.whenTargetNamed('fr');
container
.bind<Intl>('Intl')
.toConstantValue({ goodbye: 'au revoir' })
.whenTargetNamed('fr');
container
.bind<Intl>('Intl')
.toConstantValue({ hello: 'hola' })
.whenTargetNamed('es');
container
.bind<Intl>('Intl')
.toConstantValue({ goodbye: 'adios' })
.whenTargetNamed('es');
const fr: Intl[] = container.getAllNamed<Intl>('Intl', 'fr');
const es: Intl[] = container.getAllNamed<Intl>('Intl', 'es');
getAllNamedAsync
getAllNamedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): Promise<T[]>;
通过与其给定的命名约束匹配的运行时标识符解析所有依赖项:
const container: Container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ hello: 'bonjour' }))
.whenTargetNamed('fr');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ goodbye: 'au revoir' }))
.whenTargetNamed('fr');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ hello: 'hola' }))
.whenTargetNamed('es');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ goodbye: 'adios' }))
.whenTargetNamed('es');
const fr: Promise<Intl[]> = container.getAllNamedAsync<Intl>('Intl', 'fr');
const es: Promise<Intl[]> = container.getAllNamedAsync<Intl>('Intl', 'es');
getAllTagged
getAllTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T[];
通过与其给定的标记约束匹配的运行时标识符解析所有依赖项。所有绑定必须同步解析,否则将抛出错误:
const container: Container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container
.bind<Intl>('Intl')
.toConstantValue({ hello: 'bonjour' })
.whenTargetTagged('lang', 'fr');
container
.bind<Intl>('Intl')
.toConstantValue({ goodbye: 'au revoir' })
.whenTargetTagged('lang', 'fr');
container
.bind<Intl>('Intl')
.toConstantValue({ hello: 'hola' })
.whenTargetTagged('lang', 'es');
container
.bind<Intl>('Intl')
.toConstantValue({ goodbye: 'adios' })
.whenTargetTagged('lang', 'es');
const fr: Intl[] = container.getAllTagged<Intl>('Intl', 'lang', 'fr');
const es: Intl[] = container.getAllTagged<Intl>('Intl', 'lang', 'es');
getAllTaggedAsync
getAllTaggedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T[]>;
通过与其给定的标记约束匹配的运行时标识符解析所有依赖项:
const container: Container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ hello: 'bonjour' }))
.whenTargetTagged('lang', 'fr');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ goodbye: 'au revoir' }))
.whenTargetTagged('lang', 'fr');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ hello: 'hola' }))
.whenTargetTagged('lang', 'es');
container
.bind<Intl>('Intl')
.toDynamicValue(async () => ({ goodbye: 'adios' }))
.whenTargetTagged('lang', 'es');
const fr: Promise<Intl[]> = container.getAllTaggedAsync<Intl>(
'Intl',
'lang',
'fr',
);
const es: Promise<Intl[]> = container.getAllTaggedAsync<Intl>(
'Intl',
'lang',
'es',
);
isBound
isBound(serviceIdentifier: interfaces.ServiceIdentifier<unknown>): boolean;
您可以使用 isBound 方法检查是否存在给定服务标识符的已注册绑定。
interface Warrior {
kind: string;
}
const katanaSymbol: symbol = Symbol.for('Katana');
const warriorSymbol: symbol = Symbol.for('Warrior');
@injectable()
class Ninja implements Warrior {
public readonly kind: string = 'ninja';
}
@injectable()
class Katana {}
const container: Container = new Container();
container.bind<Warrior>(Ninja).to(Ninja);
container.bind<Warrior>(warriorSymbol).to(Ninja);
// returns true
const isNinjaBound: boolean = container.isBound(Ninja);
// returns true
const isWarriorSymbolBound: boolean = container.isBound(warriorSymbol);
// returns false
const isKatanaBound: boolean = container.isBound(Katana);
// returns false
const isKatanaSymbolBound: boolean = container.isBound(katanaSymbol);
isCurrentBound
isCurrentBound(serviceIdentifier: interfaces.ServiceIdentifier<unknown>): boolean;
您可以使用 isCurrentBound 方法检查是否仅在当前容器中存在给定服务标识符的已注册绑定。
interface Warrior {
kind: string;
}
const katanaSymbol: symbol = Symbol.for('Katana');
const warriorSymbol: symbol = Symbol.for('Warrior');
@injectable()
class Ninja implements Warrior {
public readonly kind: string = 'ninja';
}
@injectable()
class Katana {}
const container: Container = new Container();
container.bind<Warrior>(Ninja).to(Ninja);
container.bind<Warrior>(warriorSymbol).to(Ninja);
const containerChild: Container = container.createChild();
containerChild.bind<Katana>(Katana).to(Katana);
containerChild.bind<Katana>(katanaSymbol).to(Katana);
// returns false
const isNinjaBound: boolean = containerChild.isCurrentBound(Ninja);
// returns false
const isWarriorSymbolBound: boolean =
containerChild.isCurrentBound(warriorSymbol);
// returns true
const isKatanaBound: boolean = containerChild.isCurrentBound(Katana);
// returns true
const isKatanaSymbolBound: boolean =
containerChild.isCurrentBound(katanaSymbol);
isBoundNamed
isBoundNamed(serviceIdentifier: interfaces.ServiceIdentifier<unknown>, named: string): boolean;
您可以使用 isBoundNamed 方法检查是否存在具有给定命名约束的给定服务标识符的已注册绑定。
const divisor: string = 'divisor';
const invalidDivisor: string = 'InvalidDivisor';
const validDivisor: string = 'ValidDivisor';
const container: Container = new Container();
container
.bind<number>(divisor)
.toConstantValue(0)
.whenTargetNamed(invalidDivisor);
// returns true
const isDivisorBoundInInvalidDivisorName: boolean = container.isBoundNamed(
divisor,
invalidDivisor,
);
container
.bind<number>(divisor)
.toConstantValue(1)
.whenTargetNamed(validDivisor);
// returns true
const isDivisorBoundInValidDivisorName: boolean = container.isBoundNamed(
divisor,
validDivisor,
);
isBoundTagged
isBoundTagged(serviceIdentifier: interfaces.ServiceIdentifier<unknown>, key: string, value: unknown): boolean;
您可以使用 isBoundTagged 方法检查是否存在具有给定标记约束的给定服务标识符的已注册绑定。
const divisor: string = 'divisor';
const container: Container = new Container();
container
.bind<number>(divisor)
.toConstantValue(0)
.whenTargetTagged('isValidDivisor', false);
// returns true
const isDivisorBoundInIsValidDivisorFalseTag: boolean = container.isBoundTagged(
divisor,
'isValidDivisor',
false,
);
container
.bind<number>(divisor)
.toConstantValue(1)
.whenTargetTagged('isValidDivisor', true);
// returns true
const isDivisorBoundInIsValidDivisorTrueTag: boolean = container.isBoundTagged(
divisor,
'isValidDivisor',
true,
);
load
load(...modules: interfaces.ContainerModule[]): void;
调用每个模块的注册方法。请参阅 ContainerModule API 文档
loadAsync
loadAsync(...modules: interfaces.AsyncContainerModule[]): Promise<void>;
与 load 相同,但用于异步注册。
rebind
rebind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>;
替换给定 serviceIdentifier 的所有现有绑定。
该函数返回 BindingToSyntax 的实例,允许创建替换绑定。
const serviceId: string = 'serviceId';
const container: Container = new Container();
container.bind<number>(serviceId).toConstantValue(1);
container.bind<number>(serviceId).toConstantValue(2);
// returns [1, 2]
const valuesBeforeRebind: number[] = container.getAll(serviceId);
container.rebind<number>(serviceId).toConstantValue(3);
// returns [3]
const valuesAfterRebind: number[] = container.getAll(serviceId);
rebindAsync
rebindAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): Promise<interfaces.BindingToSyntax<T>>;
这是 rebind 的异步版本。如果您知道停用是异步的,则应使用此版本。
resolve
resolve<T>(constructor: interfaces.Newable<T>): T;
Resolve 的工作方式与 container.get 相同,但如果未找到所提供类型的绑定,则会自动向容器添加绑定。
import { Container, injectable } from 'inversify';
@injectable()
class Katana {
public hit() {
return 'cut!';
}
}
@injectable()
class Ninja implements Ninja {
public katana: Katana;
constructor(katana: Katana) {
this.katana = katana;
}
public fight() {
return this.katana.hit();
}
}
const container: Container = new Container();
container.bind(Katana).toSelf();
// Ninja is provided and a Ninja bindong is added to the container
const ninja: Ninja = container.resolve(Ninja);
请注意,它只允许跳过在依赖关系图(组合根)中声明根元素的绑定。所有子依赖项(例如前面示例中的 Katana)都需要声明绑定。
onActivation
onActivation<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, onActivation: interfaces.BindingActivation<T>): void;
为与服务标识符关联的所有服务添加激活处理程序。
interface Weapon {
damage: number;
}
export class Katana implements Weapon {
#damage: number = 10;
public get damage(): number {
return this.#damage;
}
public improve(): void {
this.#damage += 2;
}
}
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana);
container.onActivation(
'Weapon',
(_context: interfaces.Context, katana: Katana): Katana | Promise<Katana> => {
katana.improve();
return katana;
},
);
// Katana.damage is 12
const katana: Weapon = container.get<Weapon>('Weapon');
onDeactivation
onDeactivation<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, onDeactivation: interfaces.BindingDeactivation<T>): void;
为服务标识符添加停用处理程序。
interface Weapon {
damage: number;
}
export class Katana implements Weapon {
readonly #damage: number = 10;
public get damage(): number {
return this.#damage;
}
}
const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inSingletonScope();
container.get('Weapon');
container.onDeactivation('Weapon', (weapon: Weapon): void | Promise<void> => {
console.log(`Deactivating weapon with damage ${weapon.damage.toString()}`);
});
container.unbind('Weapon');
restore
restore(): void;
将容器状态恢复到上次快照。有关更多信息,请参阅 文档。
snapshot
snapshot(): void;
保存容器的状态以便稍后使用 restore 方法恢复。有关更多信息,请参阅 文档。
tryGet
tryGet<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T | undefined;
与 container.get 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 undefined。
tryGetAsync
tryGetAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): Promise<T | undefined>;
与 container.getAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<undefined>。
tryGetNamed
tryGetNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T | undefined;
与 container.getNamed 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 undefined。
tryGetNamedAsync
tryGetNamedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): Promise<T | undefined>;
与 container.getNamedAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<undefined>。
tryGetTagged
tryGetTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T | undefined;
与 container.getTagged 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 undefined。
tryGetTaggedAsync
tryGetTaggedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T | undefined>
与 container.getTaggedAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<undefined>。
tryGetAll
tryGetAll<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, options?: interfaces.GetAllOptions): T[]
与 container.getAll 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 []。
tryGetAllAsync
tryGetAllAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, options?: interfaces.GetAllOptions): Promise<T[]>;
与 container.getAllAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<[]>。
tryGetAllNamed
tryGetAllNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T[];
与 container.getAllNamed 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 []。
tryGetAllNamedAsync
tryGetAllNamedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): Promise<T[]>;
与 container.getAllNamedAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<[]>。
tryGetAllTagged
tryGetAllTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T[];
与 container.getAllTagged 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 []。
tryGetAllTaggedAsync
tryGetAllTaggedAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T[]>;
与 container.getAllTaggedAsync 相同,但在没有绑定绑定到 serviceIdentifier 的情况下返回 Promise<[]>。
unbind
unbind(serviceIdentifier: interfaces.ServiceIdentifier): void;
删除此容器中绑定到服务标识符的所有绑定。这将导致 停用过程。
unbindAsync
unbindAsync(serviceIdentifier: interfaces.ServiceIdentifier<unknown>): Promise<void>;
这是 unbind 的异步版本。如果与此服务标识符相关的任何停用是异步的,则应使用此方法而不是 container.unbind。
unbindAll
unbindAll(): void;
删除此容器中绑定的所有绑定。这将导致 停用过程。
unbindAllAsync
unbindAllAsync(): Promise<void>;
这是 unbindAll 的异步版本。如果容器的任何停用是异步的,则应使用此方法而不是 container.unbindAll。
unload
unload(...modules: interfaces.ContainerModuleBase[]): void;
删除模块添加的绑定和处理程序。这将导致 停用过程。 请参阅 ContainerModule API 文档。
unloadAsync
unloadAsync(...modules: interfaces.ContainerModuleBase[]): Promise<void>;
unload 的异步版本。如果任何容器模块的停用是异步的,则应使用此方法而不是 container.unload。
parent
parent: Container | null;
访问父容器。
id
id: number;
容器的唯一标识符。