Introduction of Environment file In Angular2
Posted By : Anchal Gaur | 24-Apr-2018
In Angular2 applications, Setting up the configuration is a piece of almost every undertaking project. The recent framework enables you to manage configuration which is shared the overall environment and which is separate for each environment, as for "production", "development" and vice versa. Node.js as of now gives a perfect mechanism to read JSON records, you can just require the JSON config files. I have tried to make the environment files separately for a different environment in my application.
I am seperating this tutorial into three parts.
(1) Setting up:- config class and json files to load these files.
(2) Pre-loading:- By making a custom router outlet.
(3) Usage:- In config file of the application.
1. Setting Up:-
This starts by making a config folder in app directory or we can make a seperate environment file. In my case, it is like /src/environment. Inside it, I created three environment files seperatly for local, development and production which will hold the current environment and shareable configuration. for e.g.
{
"env": "local"
}
for development
{
"env": "dev"
}
for prod
{
"env": "prod"
}
config class:- In the next step, a file named config.ts is created in the same folder. I am using angular2 typescript so that's why it's extension is '.ts'. This file will be used in two ways, one for load the required files and second to retreive the config variables.
import { Injectable } from ‘angular2/core’;
@Injectable()
export class Config {
private _config: Object
private _env: Object
constructor(private http: Http) {
}
load() {
// json files will be loaded here
}
getEnv(key: any) {
return this._env[key];
}
get(key: any) {
return this._config[key];
}
};
There are two particular properties _config and _env. What's more, two open methods getEnv() and get(). The reason is, all shareable setup i.e. 'env.json' information will be held in
After that we will write the code to load the required files, so we are using angular2's built-in '
Since in angular2 application
load() {
return new Promise((resolve, reject) => {
this.http.get(‘src/app/config/env.json’)
.map(res => res.json())
.subscribe((env_data) => {
this._env = env_data;
this.http.get(‘src/app/config/’ + env_data.env + ‘.json’)
.map(res => res.json())
.catch((error: any) => {
console.error(error);
return Observable.throw(error.json().error || ‘Server error’);
})
.subscribe((data) => {
this._config = data;
resolve(true);
});
});
});
}
In the above code, we are making two requests one after another; one to load env.json and second to load the required environment .json file.
2. Pre-loading:-
Actually what we want, is to load the required config files before any of the components is initialized including our main app component which is used for bootstrap and to make sure all the config is available as a service across the application.
There is
Load files inside bootstrap().then() function.
Load files in a part's @CanActivate explanation.
Utilize custom RouterOutlet which will stack the records before course actuation
Let's go deep in the working of each solution.
The issue in the first solution is that the bootstrap is settled after the
3. Usage:-
Usage is very simple and easy
import {Config} from ‘../config/config’;
@Component({
selector: ‘sample’
})
export class SampleComponent {
constructor(private _config: Config) {
var url = _config.get('apiUrl')
console.log(url)
var env = _config.getEnv('env')
console.log(env)
}
}
1. we can use cache to store and read config files otherwise
2. config class can be used as a registry, simply create a getter and setter methods.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anchal Gaur
Anchal has a good knowledge of HTML,CSS and Javascript. She is working here as a UI Developer. Her hobbies are travelling and to read novels in free time.