gpt4 book ai didi

angular - 循环依赖注入(inject) Angular 2

转载 作者:太空狗 更新时间:2023-10-29 17:00:42 25 4
gpt4 key购买 nike

我一直在努力将服务注入(inject)彼此。以下博客Circular Dependency in constructors and Dependency Injection它说的有点令人困惑

One of the two objects is hiding another object C

我在相互注入(inject)服务类时出现以下错误

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);
}
}

非常感谢您的评论和回答。

谢谢

最佳答案

您可以通过注入(inject) Injector 而不是导致循环依赖的服务之一来解决循环依赖

private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}

关于angular - 循环依赖注入(inject) Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40525850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com