This question already has an answer here:
I have several settings which should be in a configuration file.
For example: URL of APIs
Where is the best place for it in Ionic 2?
This question already has an answer here:
I have several settings which should be in a configuration file.
For example: URL of APIs
Where is the best place for it in Ionic 2?
Save them in a class singleton ( generally an anti-pattern) or even better Namespace equivalent.
Namespace equivalent
From Angular 2/4 Docs:
So you would need to define a config object with the urls and so on, and then an OpaqueToken to be able to use it when injecting the object with your configuration.
I included all my configuration in the
app-config.ts
fileWhat
OpaqueToken
is may be confusing at first, but it just a string that will avoid naming conflicts when injecting this object. You can find an amazing post about this here.Then, you just need to include it in the page you need it like this:
Please notice how to include it in the
providers
arrayAnd how to tell the injector how it should obtain the instance of the config object
UPDATE
OpaqueToken
has been deprecated since v4.0.0 because it does not support type information, useInjectionToken<?>
instead.So instead of these lines:
Now we should use
You may use either WebSQL or SQLite table or LocalStorage since both methods are very well supported by Ionic and hybrid Apps frameworks.