2016-11-10 12 views
9

私はservicesをお互いに注入するのに苦労してきました。それは二つのオブジェクトの円の依存性注入角度2

一つは互いに

にサービスクラスを注入しながら

は、私は次のエラーを取得する別のオブジェクトCを隠していると言うところ次のブログCircular Dependency in constructors and Dependency Injectionは一種の混乱されます

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

あなたのコメントと回答は非常に高く評価されます。

おかげ

答えて

20

あなたは、私が `InjectorServiceを削除したInjectorの代わりに、循環依存

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

'最大コールスタックサイズexceeded' –

+0

を引き起こすサービスのいずれかを注入することにより、循環依存を回避することができます'インジェクターを介して両方のサービスを注入しようとしました –

+0

これは機能しましたか?サイクルを中断するために 'setTimout()'が必要かもしれません(覚えていない)。 –

関連する問題