How do you use external environment variables in the typescript files

How do you use external environment variables in the typescript files. For example how do you hide the actual key from inside the constants.ts file and use an environment variable instead? So basically how do you use environment variables in typescript files?

I think the simplest means would be adding a npm task to write a file. Before you deploy run that script to create the file. If the project uses version control ignore that file. You could also create a service that fetches the data from the server-side before app set-up (Angular).

APP_INITIALIZER is a special provider in Angular that will halt the app set-up until the data is fetched from that provider. In the project I’m working on they have implemented that to fetch a bunch of enums before starting up the app. In a similar way you could do the same creating an end-point to give you the environment data.

I am new to angular so would please provide an example of what you are referring to?

I think this something along the lines which I’m referring to.

I found that by searching google with the below search query.

“writing environment variables to a file during ng serve”

Te other methods something like searching this in google.

“Angular APP_INITIALIZER to load config”

I’ve been using php for a decade and JavaScript the same. A lot of this stuff just comes down to knowing what to search in google. I’m fairly new to Angular and node myself.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.