I have a single json file located at the root directory called config.json:
{ "base_url": "http://localhost:3000" }
Within my service class, I am looking to utilize it in this manner:
private productsUrl = config.base_url + 'products';
I have come across numerous articles offering solutions that either involve sending a http.get request to fetch the file and extract the variable or outdated methodologies for angular.js (angular 1).
I find it hard to believe there isn't a simpler way to incorporate this existing file without having to make an additional server request.
In my opinion, I would expect the bootstrapping function to provide such functionality, something like:
platformBrowserDynamic().bootstrapModule(AppModule, { config: config.json });
Although functional, this is not the most optimal solution:
export class Config {
static base_url: string = "http://localhost:3004/";
}
You can then use it as needed:
private productsUrl = Config.base_url + 'products';
This method is not ideal as it requires creating the class (or updating properties) in a build script. This is exactly what I was hoping to avoid by using the config.json file.
Personally, I prefer the config.json approach as it is less invasive with the TypeScript compiler. Any suggestions on how to achieve this are greatly welcomed and appreciated!