前端开发过程当中,咱们经常须要根据需求去运行或者打包不一样环境的代码,幸运的是,angular给咱们提供了environments配置,可是angular6.x的配置方式和angular的其余版本的配置方式是有所不一样的,下面我就分别介绍在测试test、预生产pre环境下实现environments配置。前端
首先,在environments文件夹下建立environment.test.ts和environment.pre.ts文件,以下图json
而后分别在里面添加以下内容测试
而后找到angular.json文件,在architect对象下的configurations下添加以下代码:spa
而后在找到serve,添加以下的代码code
能够经过运行ng serve --configuration=test或者ng serve -c test课件在控制台打印:对象
{production: false, path: "xxxxx"} path: "xxxxxx" production: false __proto__: Object
同理运行ng serve --configuration=pre或者ng serve -c pre可获取到预生产环境的域名,因此,你只须要在须要用到环境变量的地方引入便可,如图片
import {environment} from '../environments/environment';
一样的,首先在environments建立environment.test.ts和environment.pre.ts,而后按照上文的步骤,在里面添加相同的内容,而后去到angular-cli.json文件找到environments,在里面添加如何内容:ip
而后经过运行ng s --env=test就能够运行测试环境了,在须要的地方引入environment便可。开发