I have a provider which uses another provider, and I am getting this error when viewing the app through ionic serve:
Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using ‘barrel’ index.ts files
I have read that it can be fixed using forwardref, but the examples seem very different to what I am trying to do.
Does forwardref need to go in app.module.ts somewhere?
My personal opinion is: most of the time, if you need to use forwardRef, you designed something incorrectly. Circular dependencies can cause bugs that are extremely hard to track, even with a time traveler. How would you diagram your injections? If it’s A -> B -> A, then you might want to rethink.
Hi thanks for the reply, no it’s just A->B. The page imports provider A, and provider A imports provider B. Am I right in thinking this is a normal implementation?
I have declared/imported them at the top of app.module.ts, then added them into providers in ngModule. Then in the provider files and page files, imported them at the top and added into the constructors.
So those stack overflow answers are incorrect?
It is strange that it has been working for me with one provider, but started giving me the error when I added another.
It’s a common pattern IMO. The framework changes a lot, so a lot of half-answers time out after a few months. In this case, though, the issue is Angular, not Ionic. If you put a provider in a local module and also into app.module.ts, you break what Angular is expecting, and it defines the provider as both a global and a local provider, which will lead to bad behavior.
Crawl @mhartington’s repo if you want code samples that work. For example:
import { SpotifyProvider } from '../../providers/spotify/spotify'
...
export class HomePage {
...
constructor(
public navCtrl: NavController,
public fb: FormBuilder,
public spotify: SpotifyProvider
)
...
No I haven’t changed the page module files, the only difference to the above example is that I am trying to access one of my providers in another of my providers.
Sure, I’ll post code. Sorry, I was trying to be as clear as possible, to be fair, you were the one that misinterpreted what I had said, SigmundFroyd understood me well enough.