3

I'm building an Ionic 3 app (uses Angular 4). I've created a LocationProvider that will allow me to extract location services from the actual page logic so that it's reusable.

My LocationProvider uses the @ionic-native/geolocation (the Geolocation provider).

When I inject my LocationProvider into a page via @NgModule({ providers: [LocationProvider] }), I get an error saying Geolocation provider cannot be found. Is there any way to include the Geolocation provider whenever the LocationProvider is used? Or do I need to always have providers:[LocationProvider, Geolocation]?

I'm guessing this is not Ionic specific (but Angular 4), is there any way around needing to list both providers in NgModule?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Matt
  • 1,392
  • 12
  • 24

1 Answers1

1

providers accepts both single providers and provider arrays.

it can be

export const LOCATION_PROVIDERS = [LocationProvider, Geolocation];
...
providers: [LOCATION_PROVIDERS]
...

Providers can also be wrapped with their own module and imported.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • When you say "Providers can also be wrapped...", do you mean exporting `@NgModule({ providers: [LocationProvider, Geolocation]})`? Then importing that via `@NgModule({ imports: [LocationProviderModule]})`? Do I need to worry about that causing Geolocation to be imported 10 times or something? – Matt Jul 20 '17 at 13:28
  • That's correct. Unless you're dealing with lazy loaded modules, you have nothing to worry about. All regular modules share same injector. See https://stackoverflow.com/questions/39031284/angular-2-injector-hierarchy-and-ngmodule for reference. – Estus Flask Jul 20 '17 at 13:50
  • That makes sense. Do I then need to worry about values stored by the child injector being different from the main injector? (can't find much on this for ng4, just seem to be finding original ng1 stuff) – Matt Jul 20 '17 at 14:18
  • Unless you're defining these providers in child injector `providers` (lazy loaded modules, components, etc), there should be no problems. – Estus Flask Jul 20 '17 at 14:22
  • Last question. Is there anything inherently bad about using any of these solutions? Using `LOCATION_PROVIDERS` or wrapping it in a module both seem to be simpler initially, but they also seem to require knowledge of the project (bad for "that next poor schmuck who's gonna work on the code"). Importing `LocationProvider` and `Geolocation` is the one that seems to be simplest for anyone who comes along (especially since it gives you a nice error when you do it wrong) and you're least likely to have different styles of importing across different classes (and from different developers). – Matt Jul 20 '17 at 14:56
  • If you're publishing self-contained package or reuse code in several projects, a NgModule is appropriate. Otherwise packing providers into LOCATION_PROVIDERS locally is perfectly ok, provider constants were used in Angular 2 source code before the introduction of NgModule. – Estus Flask Jul 20 '17 at 15:34