繁体   English   中英

如何在提供者的useFactory中使用单件服务/类:app.module中的[]

[英]How to use singleton service/class in useFactory in providers:[] in app.module

我是父抽象服务

    @Injectable()
    export abstract class parent {}

和两个儿童服务

    @Injectable()
    export class dataService extends parent {}

    @Injectable()
    export class mockService extends parent {}

现在我想在app.module中使用useFactory基于一些常量变量(即productionStatus),这意味着如果状态已连接,则使用'dataservice',否则使用'mockservice'

这就是我现在正在做的事情

core.module:

@NgModule({
  providers: [
    {
      provide: PayoutService,
      useFactory:()=> new FactoryConfigService().getPayoutServiceInstance()
    },
    {
      provide: OrderService,
      useFactory:()=> new FactoryConfigService().getOrderServiceInstance()
    }
  ]
})

FactoryConfigService:

export class FactoryConfigService {
  public productionStatus: Connection.Status = Connection.Status.disconnected;

  constructor() { }

  getOrderServiceInstance() {
    if (this.productionStatus == Connection.Status.connected) {
      return new OrderDataService();
    } else {
      return new OrderMockService();
    }
  }
  getPayoutServiceInstance() {
    if (this.productionStatus == Connection.Status.connected) {
      return new PayoutDataService();
    } else {
      return new PayoutMockService();
    }
  }

}

现在我的问题是如何在不含'new'的providers []和singleton / injectable中使用FactoryConfigService

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM