在构建Angle 6项目时从服务更改URL [英] Change url from service when build angular 6 project
问题描述
在开发中,我在角度服务中使用 http://localhost:565565/api/v2
从本地服务器检索数据.
In development, I use http://localhost:565565/api/v2
in an angular service to retrieve data from local server.
现在已经投入生产,我想将该网址更改为 https://www.data.com/api/v2
.
Now in production, I want to change that url to https://www.data.com/api/v2
.
如何用angluar cli进行更改?我不想手动更改大型 main.xxxxxxx.js
文件.
How to change with angluar cli ? I don't want manually change the big main.xxxxxxx.js
file.
直到现在我使用 ng build --prod
,但这还不够.
Until now I use ng build --prod
but it is not enough.
推荐答案
使用angular.cli中提供的环境:
Use the environments provided in angular.cli:
用于开发的environment.ts上
On environment.ts for development use
export const environment = {
production: false,
apiUrl: 'http://localhost:565565/api/v2'
};
用于生产的environment.prod.ts:
On environment.prod.ts for production use:
export const environment = {
production: true,
apiUrl: 'https://www.data.com/api/v2'
};
然后,根据运行应用程序的环境,将使用相应的文件.如果您运行ng serve环境,则将使用.ts,如果您运行ng build,则将使用--prod environment.prod.ts.
Then depending on which environment you run your app the corresponding file will be used. If you run ng serve environment.ts will be used, if you run ng build --prod environment.prod.ts will be used.
另外,您需要将环境导入到ts才能使用apiUrl
Additionally you will need to import environment to your ts to use apiUrl
ts中的示例
import { environment } from './../environments/environment';
并将其用作 environment.apiUrl
这篇关于在构建Angle 6项目时从服务更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!