Environment is for you to define your variables that change between development and production
They work very simply, they are two files, environment.ts
for development and environment.prod.ts
for production, in them you export an object that will contain the data, these two objects must have the same keys but with different values, for example
environment.ts
:
export const environment = {
production: false,
api: 'localhost:3000'
}
environment.prod.ts
:
export const environment = {
production: true,
api: 'api.example.com'
}
To use just import the file and use the properties of the exported object:
import { Component } from '@angular/core';
//A url é essa, não coloque '.ts' ou '.prod.ts'
import { environment } from './../environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
console.log(environment);
}
}
When you are in development environment (ng server
) Angular will use the file environment.ts
, with the production build (ng build --prod
) he will use the environment.prod.ts
So "deschumbe"!
– Wallace Maxters
The URL will be informed by the user ? In this case, configured.
– Lucas Brogni