我一直在为给对方注射services而苦苦挣扎。下面的博客构造函数中的循环依赖与依赖注入有点让人困惑,它说
这两个对象中的一个正在隐藏另一个对象C
在将服务类插入到彼此之间时,我会得到以下错误
不能解析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);
}
}您的意见和答复将不胜感激。
谢谢
发布于 2016-11-10 11:01:57
您可以通过注入Injector (而不是导致循环依赖的服务之一)来解决循环依赖关系。
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}发布于 2018-03-26 14:37:52
在我的例子中( Ionic项目中的角4),甚至在服务使用之前(在用户交互时)就注入了服务(相同的“不能解决所有参数.”)。在启动时)。
对我来说起作用的是注入(从而提供)名为的服务。
所以在我的应用模块中:
...
providers: [{provide: "MyServiceName", MyServiceClass}],
...必要时:
const myService: MyServiceClass = injector.get("MyServiceName");
...发布于 2018-08-25 05:01:52
我知道这篇文章很旧,但是感谢上面的解决方案,因为我有循环依赖问题,所以我不得不使用注入器来避免创建中介服务来进行通信,尽管在上面的解决方案中有一处更正。
从5号角开始,我们需要使用:
提供者:{ providers:"MyServiceName",useclass: MyServiceClass},
然后
injector.get('MyServiceName')
虽然get被剥夺了
https://stackoverflow.com/questions/40525850
复制相似问题