Cle*_*ode 13 dependency-injection circular-dependency angular
我一直在努力services互相注入.构造函数和依赖注入中的以下博客循环依赖关系在它所说的地方有点令人困惑
两个对象之一是隐藏另一个对象C.
我在将Service类注入彼此时遇到以下错误
无法解析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);
}
}
Run Code Online (Sandbox Code Playgroud)
您的意见和回答将受到很多赞赏.
谢谢
Gün*_*uer 28
您可以通过注入Injector而不是导致循环依赖关系的其中一个服务来解决循环依赖关系
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9357 次 |
| 最近记录: |