Skip to content

Commit 6a82799

Browse files
markpritchettfilipesilva
authored andcommitted
docs(@angular/cli): add application environments (#6068)
add instructions on how to setup application environments
1 parent b11d560 commit 6a82799

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

docs/documentation/stories.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
- [Corporate Proxy](stories/using-corporate-proxy)
2323
- [Internationalization (i18n)](stories/internationalization)
2424
- [Serve from Disk](stories/disk-serve)
25-
- [Code Coverage](stories/code-coverage)
25+
- [Code Coverage](stories/code-coverage)
26+
- [Application Environments](stories/application-environments)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Application Environments
2+
3+
## Configuring available environments
4+
5+
`.angular-cli.json` contains an **environments** section. By default, this looks like:
6+
7+
``` json
8+
"environments": {
9+
"dev": "environments/environment.ts",
10+
"prod": "environments/environment.prod.ts"
11+
}
12+
```
13+
14+
You can add additional environments as required. To add a **staging** environment, your configuration would look like:
15+
16+
``` json
17+
"environments": {
18+
"dev": "environments/environment.ts",
19+
"staging": "environments/environment.staging.ts",
20+
"prod": "environments/environment.prod.ts"
21+
}
22+
```
23+
24+
## Adding environment-specific files
25+
26+
The environment-specific files are set out as shown below:
27+
28+
```
29+
└── src
30+
└── environments
31+
├── environment.prod.ts
32+
└── environment.ts
33+
```
34+
35+
If you wanted to add another environment for **staging**, your file structure would become:
36+
37+
```
38+
└── src
39+
└── environments
40+
├── environment.prod.ts
41+
├── environment.staging.ts
42+
└── environment.ts
43+
```
44+
45+
## Amending environment-specific files
46+
47+
`environment.ts` contains the default settings. If you take a look at this file, it should look like:
48+
49+
``` TypeScript
50+
export const environment = {
51+
production: false
52+
};
53+
```
54+
55+
If you compare this to `environment.prod.ts`, which looks like:
56+
57+
``` TypeScript
58+
export const environment = {
59+
production: true
60+
};
61+
```
62+
63+
You can add further variables, either as additional properties on the `environment` object, or as separate objects, for example:
64+
65+
``` TypeScript
66+
export const environment = {
67+
production: false,
68+
apiUrl: 'http://my-api-url'
69+
};
70+
```
71+
72+
## Using environment-specific variables in your application
73+
74+
Given the following application structure:
75+
76+
```
77+
└── src
78+
└── app
79+
├── app.component.html
80+
└── app.component.ts
81+
└── environments
82+
├── environment.prod.ts
83+
├── environment.staging.ts
84+
└── environment.ts
85+
```
86+
87+
Using environment variables inside of `app.component.ts` might look something like this:
88+
89+
``` TypeScript
90+
import { Component } from '@angular/core';
91+
import { environment } from './../environments/environment';
92+
93+
@Component({
94+
selector: 'app-root',
95+
templateUrl: './app.component.html',
96+
styleUrls: ['./app.component.css']
97+
})
98+
export class AppComponent {
99+
constructor() {
100+
console.log(environment.production); // Logs false for default environment
101+
}
102+
title = 'app works!';
103+
}
104+
```
105+
106+
## Environment-specific builds
107+
108+
Running:
109+
110+
```
111+
ng build
112+
```
113+
114+
Will use the defaults found in `environment.ts`
115+
116+
Running:
117+
118+
```
119+
ng build --env=staging
120+
```
121+
122+
Will use the values from `environment.staging.ts`

0 commit comments

Comments
 (0)