If you wish to run angular in production or development mode with single ng serve command
Read on...
Step 1:
$ ng generate environments
This command generates these files;
- src\environments\environment.ts
- src\environments\environment.prod.ts
- src\environments\environment.development.ts
Step 2:
Edit angular.json (found in same level as package.json of your project)
Add following lines in projects>architect>build>configurations>production:
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
You may notice i skip the development part, because this is already added, when you run 'ng generate environments'.
Step 3:
In your component class
import { environment } from 'src/environments/environment';
And, also add
navBgColor = environment.bgColor;
Run ng serve commands
- For Production
ng serve -c production --open
- For Development
ng serve -c development --open
Logic explained:
For 'Development'
environment.ts will be replaced by environment.development.ts
For 'Production'
environment.ts will be replaced by environment.prod.ts
Note that, to see the difference, the value 'bgColor' has to be different, between environment.prod.ts and environment.development.ts.
Example:
environment.prod.ts:
export const environment = {
bgColor: 'red',
production: true
};
environment.development.ts:
export const environment = {
bgColor: 'cyan',
production: false
};