I use Intellij Ultimate to code my angular 4 application.
I created a new Angular 4 project, it contains environment.ts
and environment.prod.ts
and the environments are properly configured in angular-cli.json
.
how do I import it in my code? Since actually when I build it I state which environment to use. How does it work? Do I need to compile something with Intellij?
I tried googling and found many examples when people actually imported a specific environment.ts file. but that's not good, right? Since it will use the same environment.ts file even if I build for a different environment.
What do I do?
Environments wasn't working for me with
@angular/cli
1.2.4
, but they did load Ok with1.3.2
, so be sure you use the latest version of the series.I had this same question, and found the same article that @Ahmed posted. However, that didn't fix my problem: I just got:
I'm working with a build created via
ng new
, so if you are working with the latest angular client you will likely have the same problem I had. As @Ahmed states in his comment on his answer, it is important that you get the relative path correct. The (current) version of the ng cli puts your main application component here:And it places the environment file here:
Therefore, the correct line to include the environment from inside your app.component is this:
Small "feature":
environment.ts
is actually the environment configuration file for the development environment. However, at run time (either due to theng build
command orng serve
command), this file will be replaced by whatever the actual environment states. So even though you are explicitly including the development environment file, at run time you will get the environment variables for whatever your environment is. It seems strange to me, but it works, and it seems like that is how it is designed to work.Here is a really good article on environment files with angular cli: http://tattoocoder.com/angular-cli-using-the-environment-option/
In summary, you do imported
environment.ts
but the correct file will be imported depending on what environment it is. angular cli will take care of that as explained in the article.I had lot of trouble getting this to work. Tried to follow so many tutorials, ng serve just didn't pickup anything outside of dev environment. I was finally able to make it work with following:
.angular-cli.json file
Environment files, under ./src and following folder structure
environment.ts file:
environment.test.ts file
environment.prod.ts file
**Do not create another environment.ts file under src.
app.module.ts or any other components where you want to use the environment properties. Remember to import environment from ../environments folder. I made a mistake of following a tutorial and creating environment.ts under src folder which did not work for me.
Hope this helps.
By the way, i did this with Angular 5.