Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove circular dependency of providers in ionic 3 app

I am including a provider in another provider in ionic 3 app. But it is giving me error 'No Provider Found'. I did some research and found that it is due to circular dependency. How can I overcome circular dependency in ionic 3 app?

like image 784
Ajay Bhakar Avatar asked Oct 24 '17 10:10

Ajay Bhakar


1 Answers

Angular doesn't allow referencing a provider in another one as it may lead to circular dependance injection. The way I solved it was removing declaration of class variable from the constructor and using Injector from angular core to inject dependancy in timeout using the following code:

So suppose you want to include Provider A in Provider B,

import { ProviderA } from '../provider-a/provider-a';

export class ProviderB {
  provider_a:any;
  constructor(public injector: Injector) {
    console.log('Hello ProviderB Provider');
    setTimeout(() => this.provider_a = injector.get(ProviderA));
  }
}
like image 108
karan sharma Avatar answered Oct 13 '22 11:10

karan sharma