Circular dependency injection angular 2 -
i've been struggling around injecting services
each other. following blog circular dependency in constructors , dependency injection kind of confusing says
one of 2 objects hiding object c
i following error while injecting service class each other
can't resolve parameters 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 , answered appreciated lot.
thanks
you can workaround circular dependencies injecting injector
instead of 1 of services cause circular dependency
private payrollservice:payrollservice; constructor(/*private payrollservice:payrollservice*/ injector:injector) { settimeout(() => this.payrollservice = injector.get(payrollservice)); }
Comments
Post a Comment