Switch env variables to environment.ts
I would like to reduce some of the redundancy in mine environment.ts
, as many of my variables env
start with the same. So I wanted to do the following:
export const environment = {
base_url: '//www.myWebsite.co.uk/',
ws_base_url: this.base_url + 'ws',
download_base_url: this.base_url + 'download'
}
Instead of this:
export const environment = {
base_url: '//www.myWebsite.co.uk/',
ws_base_url: '//www.myWebsite.co.uk/ws',
download_base_url: '//www.myWebsite.co.uk/download'
}
But when I use environment.ws_base_url
, I don't get //www.myWebsite.co.uk/ws
but undefined/ws
. I'm just wondering if this really doesn't work or if I'm just missing something.
You can simply declare a variable outside the object:
const BASE_URL = '//www.myWebsite.co.uk/';
export const environment = {
base_url: BASE_URL,
ws_base_url: BASE_URL + 'ws',
download_base_url: BASE_URL + 'download'
}
Since people are demonstrating more than one way to do this, I thought I might add another way.
environment = {}
environment.base_url= '//www.myWebsite.co.uk/'
environment.ws_base_url= environment.base_url + 'ws'
environment.download_base_url= environment.base_url + 'download'
Just something I saw with your first code, it looks like you didn't include "," in your environment declaration. Right after the base_url declaration:
base_url: '//www.myWebsite.co.uk/'
also using ES5 receiver should work
const environment = {
base_url: '//www.myWebsite.co.uk/',
get ws_base_url() {
return this.base_url + 'ws';
},
get download_base_url() {
return this.base_url + 'download'
}
}
console.log(environment)
demo: https://jsbin.com/gujecaleru/edit?js,console