我一直在为注射而苦苦挣扎services
进入彼此。以下博客构造函数中的循环依赖和依赖注入 http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/它说的有点令人困惑
两个对象之一隐藏了另一个对象 C
将 Service 类相互注入时出现以下错误
无法解析 PayrollService 的所有参数:(SiteService、StorageService、
甜蜜警报服务,?)
//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);
}
}
您的评论和回答将非常感激。
Thanks
您可以通过注入来解决循环依赖问题Injector
而不是导致循环依赖的服务之一
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)