Configuration File


#1

Hello,

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


#2

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.


#3

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


#4

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”)?


#5

I wouldn’t use Http for this.

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

#6

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.


#7

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


#8

Can you please give an example of JSON file?