Where is the best place to define global variables?


#1

Where is the best place to define global variables which can be used by all providers? The base URL of REST API (http://example.com/api/v1) is an example of such variables.

My guess is that I need a provider like DataProvider or GlobalProvider which stores and exposes global data to other providers and pages. I wonder if there is any better way to do this.


#2

Sounds like a good plan to me.


#3

Don’t know if it’s the right way to do it, but I use a static for that purpose. In a separate file I’ve got something like:

export class Resources {
    static get Constants():any {
      return {
        API: {
           MY_URL: 'http://....'
         }
       }
    }
}

which I could then use like:

  Resources.Constants.API.MY_URL;

Centralize configuration for ionic 3 project
Configuration file best practices