angularionic3angular2-di

Is there a way to chain @Injectable() in Angular4


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?


Solution

  • 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.