我为我的angular2项目准备了angular-cli。
我正在通过我的angular2服务调用后端ajax服务。
对于不同的任务,我有不同的服务端点(URL)。我想使那些服务环境有意义。
假设我有两次服务
https://localhost:8080/customers
https://localhost:8080/products
既然localhost
是在我的开发环境avaliable。这是工作
现在假设x.x.x.x
是我的生产Web服务主机ip地址。
因此,对于生产环境,服务URL将为https:// xxxx:8080 / customers
请帮助我如何实现这一目标。
我发现angular-cli.json文件中有一个块
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
但是我现在确实找到了环境目录。
如何创建和管理特定于环境的端点?
应该使用ng new PROJECT_NAME命令创建两个文件:
我相信您可以手动创建它。这是生成的代码:
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `angular-cli.json`.
export const environment = {
production: false
};
您可以在两个文件中为所需的环境添加所需的配置:
// src/environments/environment.ts
export const environment = {
production: false,
apiUrl: 'http://localhost:8080'
};
...
// src/environments/environment.prod.ts
export const environment = {
production: true,
apiUrl: 'https://x.x.x.x'
};
只需使用配置:
import { environment } from '../environments/environment';
//...
let url = `${environment.apiUrl}/customers`;
只要确保您导入的是“ ../environments/environment”,而不是“ ../environments/environment.prod”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句