How to define a ServiceProvider to manage global vars?

Hi all,
I’d like to define a ServiceProvider (Singleton) that manages the global vars of the App.
It should be usable by all the other services and by the all the pages of the App.

Which is the right way to do this?
Should I import it in all pages or there is a way to define it once in the app?

I’m using Ionic 3.5.0

Thank you

Claudio

I use ngrx/store for this purpose. It’s an Angular implementation of Redux. But there’s a lot of details involved, so I recommend you read about the pros and cons of Redux before installing it yourself.

For a simpler situation: import the provider once, in app.module.ts, and declare it there as a provider. Make sure the provider has the @Injectable() decorator. Then inject the provider into any component you wish.

3 Likes