Angular 6 difference between .env vs environment.ts

给你一囗甜甜゛ 提交于 2019-12-11 08:26:51

问题


I generated a project from https://mean.io. But I am not sure what the difference is between (.env and .env.example) and (environment.ts and environment.prod.ts).

Where should I store all of my config data, secrets, and keys?


回答1:


You should store them in the environment.ts

environment.ts If you refer environment object properties in your Angular project, during development mode i.e. ng serve or ng build all values shall be read from this file.

environment.prod.ts When you build your application for production mode using ng build --prod in that case, all values of environment.ts file shall get overridden by environment.prod.ts files.

The above variables are related to your Angular application. Whereas .env and .env.example are for the Laravel application.

.env as it should be is out of version controlled and ignored when you push your project to any repository. This is for your own safety.

.env.example which contains very generic information is copied as .env on fresh install and a few changes made. ex APP_KE




回答2:


environment.ts and environment.prod.ts are used in your angular application for loading different variables depending on where the application is running.

ng serve will run the application with the environemnt.ts file. ng serve --prod will run the application with the environment.prod.ts file.

A good example of this would be for an API url. In development you would use http//:localhost:<port> where as in production the url might be www.<my-api>.com you can use the different environment files to have these switch between builds.

Keep in mind that the angular environment file will be readable to any user on you website. It's a bad idea to keep usernames, passwords or api keys in the environment.ts or environment.prod.ts files.

The .env file is used to store environment variables for the node/express API. This code runs on a server. the require('dotenv').config(); line in the server/config/config.js file is where the file is loaded.



来源:https://stackoverflow.com/questions/55554883/angular-6-difference-between-env-vs-environment-ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!