我希望每个子容器的每个类只有一个实例。
import "reflect-metadata";
import {injectable, container, singleton} from "tsyringe";
const count = {
bar: 0,
foo: 0,
fao: 0
}
@injectable()
class Foo {
constructor() {
count.foo += 1;
}
public helloWorld() {
console.log("helloworld")
}
}
@injectable()
export class Bar {
constructor(public myFoo: Foo) {
count.bar += 1;
}
}
@injectable()
export class FAO {
constructor(public myFoo: Foo) {
count.fao += 1;
}
}
const create = () => {
const childContainer = container.createChildContainer();
const myBar = childContainer.resolve(FAO);
const other = childContainer.resolve(Bar);
}
create();
create();
我想在我的 childcontainer 中有一个“singleton”。每个子容器只对所有类进行一次实例化。@singleton 仅在全局容器中解析。
我怎样才能做到这一点?