Hi All, Today we will talk about that how we can change our Angular app environment at run time when deploying the code on Heroku Server.
So, first we discuss that why we need this environment at run time. The situation is as described:
As normally every app is deployed in multiple environments e.g. staging, prod and service URL will be different for every environment. The solution for this is to have multiple environment file and choose one of them at the time of deployment dynamically.
Let’s take an example , you have this type of environment file:
This is your environment.prod.ts file and your have another api_url . So, will make another environment file as below:
We have named this file
and changed the
to http://www.yahoo.com. So, now you have three environment file. One is for your local, one is prod and another is test.
Now, the next step is how would you choose your environment file at run time. The steps for this is given below:
1. Open the file
. You will find your file like this:
We will add our new
file in environments section and then this section will we like:
2. So, first step is complete. Now, this is the time to move on next step and for that open your
file. You will find your file like this:
In the image you can see the
section and replace this as below:
3. Our application code is now complete. The next step is to create environment variable at Heroku. Open Your app on heroku go to settings.
Then choose Reveal Config Vars . You can see the image:
In the end of the image, you can find KEY and VALUE section. We will put our environment var here as:
And then click on Add button to add the variable. Now the variable is added.
The process is complete. When you will deploy your app the
command will run and will find the environment variable on heroku. Then your app will be deployed with environment.test.ts file.
If you need help deploy angular webapp on heroku you can follow this:
Previously published at https://firstname.lastname@example.org_53728/change-environment-at-runtime-in-angular-with-heroku-39478c3dfcd0
Disclaimer: The information expressed in the article belong solely to the author, and not necessarily to the author's employer, organization, committee or other group or individual.