如何与tsrynge共享同一个依赖注入树?
How to share the same dependency Injection Tree with tsrynge?
我希望每个 class 每个子容器只有一个实例。
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();
我想在我的子容器中有一个 "singleton"。所有 classes 每个子容器只实例化一次。
@singleton 仅在全局容器内解析。
我该怎么做?
子容器
中有一个方法registerSingleton
const childContainer= container.createChildContainer();
childContainer.registerSingleton(Foo);
childContainer.resolve(Bar)
我希望每个 class 每个子容器只有一个实例。
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();
我想在我的子容器中有一个 "singleton"。所有 classes 每个子容器只实例化一次。 @singleton 仅在全局容器内解析。
我该怎么做?
子容器
中有一个方法registerSingleton
const childContainer= container.createChildContainer();
childContainer.registerSingleton(Foo);
childContainer.resolve(Bar)