123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Technology,-Gadget-and-Science >> View Article

How To Have Various Environments With Angular Cli Future (angular 7)

Profile Picture
By Author: Sagar rollins
Total Articles: 2
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

As a result of this, we would need builds of different configurations: Different server URLs, different constants, different logging options, etc.

Angular CLI offers an environment feature that allows running builds targeted at specific environments for specific requirements. For instance, here is how you would run a build for production:

ng build — env=prod // up to Angular 5

Update: With Angular 6 and Above, the command is now:

ng build — configuration=production // new versions

The prod flag in the above code refers to the prod (production in v6 and above) property of the environments section of .angular-cli.json before v6:(now angular.json in v6 and above), which has two options by default: dev and prod

“environments”: {
“dev”: “environments/environment.ts”,
“prod”: “environments/environment.prod.ts”
}

You can add as many environments as you need here and name them accordingly. For instance, if you need a assesment build option, just add the following entry in .angular-cli.json:

“environments”: {
“dev”: “environments/environment.ts”,
...
... “prod”: “environments/environment.prod.ts”,
“qa”: “environments/environment.qa.ts”
}

For v6 and above, angular.json environments are now called configurations. Here is how to add a new qa environment after v6:

“configurations”: {
“production”: { … },
“qa”: {
“file replacements”: [
{
“replace”: “src/environments/environment.ts”,
“with”: “src/environments/environment.qa.ts”
}
]
}
}

Then you have to create the actual file environment.qa.ts in the environments directory.

Here is what the default environment.ts file for dev looks like:

// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build — env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
export const environment = {
production: false
};

The above environment object is where you would add any environment specific property. For instance, let’s add a server URL:

export const environment = {
production: false,
serverUrl: “http://dev.server.companyName.com"
};

Then all you have to do to provide a different URL for QA is to define that same property with the right value in environment.qa.ts:

export const environment = {
production: false,
serverUrl: “http://qa.server.companyName.com"
};

Now that your environments are defined, how do you use those properties in your code? Easy enough, all you have to do is import the environment objects as follows:

import {environment} from ‘../../environments/environment’;
@Injectable()
export class AuthService { API_URL: string = environment.server Url + ‘/theUrlForAPI’ ;

Then when you run a build for QA, Angular CLI is going to use environment.qa.ts to read the environment.server Url property value and you’re all set to deploy that build to the QA environment.

My Name is Prateek Pandey. I am a Developer, as well as consultant, and trainer for Angular where I help web developers learn and ease the stuff.

For more information click here -: https://letsmobility.com/

Total Views: 385Word Count: 415See All articles From Author

Add Comment

Technology, Gadget and Science Articles

1. Build A Successful Multi-service Platform With A Gojek Clone App
Author: Simon Harris

2. Extracting Geo-based Pricing Data Using Mobile App Scraping
Author: REAL DATA API

3. Flipkart Seller Product Data Analytics
Author: Actowiz Metrics

4. Designing Large-scale Web Scraping Systems Step By Step
Author: Web Data Crawler

5. Odoo Erp Solutions In Saudi Arabia: Transforming Saudi Businesses Digitally
Author: Andy

6. Scrape Twin Peaks Restaurants Location Data In The Usa In 2026
Author: Actowiz Solutions

7. Real-time Grocery And Food Delivery Data Apis Worldwide
Author: Retail Scrape

8. Us Pharmacy Market Data Analytics - Giants, Growth & Geography
Author: Actowiz Metrics

9. Exceptional Advantages Of Choosing Virtual Answering Services
Author: Eliza Garran

10. How Can You Use The Virtual Receptionist Service To Give Your Business The Boost It Needs?
Author: Eliza Garran

11. What Drives 42% Faster Menu Updates Through Web Scraping Japan Restaurant Menus For Pricing Insights?
Author: Retail Scrape

12. Global Custom Soc Market Is Racing Toward $43 Billion
Author: Arun kumar

13. How 82% Recruiters Rely On Job Market Data Scraping Europe For Hiring Trends 2026 For Workforce Planning?
Author: Retail Scrape

14. Step-by-step Process For Getting Your Academic Documents Translated In Birmingham
Author: premiumlinguisticservices

15. The Top Five Digital Advertising Trends
Author: Anthea Johnson

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: