圆形依赖注入角度2 [英] Circular dependency injection angular 2
问题描述
我一直在努力将服务
注入对方。以下博客构造函数中的循环依赖和依赖注入是一种令人困惑的地方,它说
两个对象之一是隐藏另一个对象C
在将服务类注入对方时,我收到以下错误
无法解析PayrollService的所有参数:(SiteService,StorageService,
SweetAlertService,?)
// abstractmodal.service.ts
@Injectable()
export abstract class AbstractModel {
abstract collection = [];
构造函数(private siteService:SiteService,private storageService:StorageService,
private sweetalertService:SweetAlertService){}
setCollectionEmpty(){
this.collection = [];
}
}
//account-payable.service.ts
@Injectable()
export class AccountPayableService extends AbstractModel {
public collection = [];
构造函数(private sS:SiteService,private stS:StorageService,private sws:SweetAlertService,
private accpPoService:PayablePurchaseOrderService,private attachmentService:AttachmentService,
private injectorService:InjectorService){
super(sS,stS,sws);
}
}
//injector.service.ts
@Injectable()
export class InjectorService {
constructor(private payrollService:PayrollService){}
cleanPayrollCollection(){
this.payrollService.setCollectionEmpty();
}
}
//payroll.service.ts
@Injectable()
export class PayrollService extends AbstractModel {
public collection = [];
构造函数(private sS:SiteService,private stS:StorageService,private sws:SweetAlertService,
private accpService:AccountPayableService){
super(sS,stS,sws);
}
}
您的意见和回答将不胜感激。 / p>
谢谢
通过注入 Injector
而不是引起循环依赖的其中一个服务来解决环境依赖关系
private payrollService:PayrollService;
构造函数(/ * private payrollService:PayrollService * / inject:Injector){
setTimeout(()=> this.payrollService = injector.get(PayrollService));
}
I've been struggling around injecting services
into each other. The following blog Circular Dependency in constructors and Dependency Injection is kind of confusing where it says
One of the two objects is hiding another object C
I get the following error while injecting Service class into each other
Can't resolve all parameters for PayrollService: (SiteService, StorageService, SweetAlertService, ?)
//abstractmodal.service.ts
@Injectable()
export abstract class AbstractModel {
abstract collection = [];
constructor(private siteService: SiteService, private storageService: StorageService,
private sweetalertService: SweetAlertService) {}
setCollectionEmpty() {
this.collection = [];
}
}
//account-payable.service.ts
@Injectable()
export class AccountPayableService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpPoService: PayablePurchaseOrderService, private attachmentService: AttachmentService,
private injectorService: InjectorService) {
super(sS, stS, sws);
}
}
//injector.service.ts
@Injectable()
export class InjectorService {
constructor(private payrollService: PayrollService) {}
cleanPayrollCollection() {
this.payrollService.setCollectionEmpty();
}
}
//payroll.service.ts
@Injectable()
export class PayrollService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpService: AccountPayableService) {
super(sS, stS, sws);
}
}
Your comments and answered will be appreciated a lot.
Thanks
You can workaround circular dependencies by injecting Injector
instead of one of the services that cause the circular dependency
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
这篇关于圆形依赖注入角度2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!