Configuration File

Hello,

What are the best practices for external configuration files in ionic 2? I want the variables to globally available.

1 Like

Do you mean you want to define some configuration values in a JSON file or something, and then have those configuration options available throughout the application? I would use a JSON file, create a provider (ConfigService), have that providers constructor load in the JSON file with Http, store those values in the provider, and then import that provider in any class that you want to access the configuration values.

2 Likes

Oh, so there is no built in way. Thanks a lot.

Iā€™m trying to implement this solution but Iā€™m having trouble retrieving the project-config.json file I created. Where can I place this file in the project so that I can access it in when I call _http.get(ā€œproject-config.jsonā€)?

I wouldnā€™t use Http for this.

export class ConfigHolder {
  config: any = require("path/relative/to/this/classes/source/file/config.json");
}

Where would you put the configuration file on iOS? On Android I put it in /sdcard folder, so I can change it manually when needed and my app can access it too. What is the right place to do it on iOS?

Thanks.

Hi
I know it may be a bit late, but the easy way should be:

Create a providers/app-config.ts file as:

export let CONFIG = { //PLEASE UPDATE THE FOLLOWING VALUES WITH YOURS ONES
appVersion: ā€œQuizionic 3.1.0ā€,
defaultUserName: ā€œStudioMobā€};

And then just import it to the file that they need such parameter as:

import {CONFIG} from ā€˜ā€¦/ā€¦/providers/app-configā€™;
ā€¦
let variable = CONFIG.appVersion;

The file is stored in app directory in this case and is read-only, like constant for your app.
Regards,
Flavio

Can you please give an example of JSON file?