Creating configuration files
In the application create a directory called config (or whatever you want) and put configuration files.
Adding configuration files to assets
Add new position to assets
in angular.json
file. That means, on after building, all configurations will be added to dist
directory.
Adding src/config
to assets
means: copy all files to the dist directory. To copy only config.json
you should
write src/config/config.json
.
Creating configuration token
InjectionToken
is a special type of token that is used when you need to provide a non-class dependency (for example a interface).
The interface should be equal to the JSON schema of the configuration file.
If you can, you can try directly import JSON file as a type thanks to TypeScript
.
More you can read on TypeScript documentation.
Loading configuration before bootstrap application
Fill created service with the downloaded configuration.
To do it, open main.ts
and add fetch
method to provide configuration as the provider.
As is evident, config.json
is downloaded and provided to the whole platform. Thanks to that, you can use
ConfigService
whenever you want.
If a browser does not support fetch
method, you should use XMLHttpRequest
.
Example of usage ConfigService
Changing configuration file
Before deploying the application you need to replace configuration files. The script should be something like that: