Angular2 如何在 prodmode 和 test 模式之间切换 baseURL [英] Angular2 How to switch baseURL between prodmode and test mode

查看:60
本文介绍了Angular2 如何在 prodmode 和 test 模式之间切换 baseURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发模式下测试我的应用程序,我全局设置了 baseURL,但现在我想启用生产模式,我想在生产模式下使用不同的 baseURL 并在两种模式(开发模式和生产模式)之间切换两个 baseURL模式)

Hi I am currently testing my app in development mode and i have baseURL globally set but now I want to enable production mode and I want to use a different baseURL in production mode and switch two baseURLs between two mode (dev mode and prod mode)

我该怎么做?

我正在使用 npm run dev-start 命令来运行开发模式.

I am using npm run dev-start command to run dev mode.

推荐答案

使用 angular cli 太容易了.首先,转到您的 .angular-cli.json 文件并编辑 environments;

It's too easy with angular cli. Firstly, go to your .angular-cli.json file and edit environments;

"environments": {
     "dev": "environment/environment.ts",
     "prod": "environments/environment.prod.ts",
     "yourCustomEnv": "environments/environment.yourCustomEnv.ts"
}

其次,在environment.yourCustomEnv.ts文件中添加api url作为属性;

Secondly, add api url as a property in environment.yourCustomEnv.ts file;

export const environment = {
     production: ...,
     apiUrl: 'youApiUrl'
}

之后,在任何你想设置api url的地方使用environment变量;

Afterwards, use environment variable wherever you want to set the api url;

import { environment } from 'pathTo/environments/environment';
export class XService {
     apiUrl = environment.apiUrl;
     ...
}

最后,使用以下命令构建您的应用程序;

Finally, build your application with the following commands;

ng build --env=yourCustomEnv ...

Angular-cli 会在您导入 environment 的每个位置自动使用 environment.yourCustomEnv.ts 文件.

Angular-cli automatically use environment.yourCustomEnv.ts file at every place where you import environment.

顺便说一句,请确保您的所有 environment 变量都具有相同的属性.

By the way, be sure that your all environment variables have same properties.

这篇关于Angular2 如何在 prodmode 和 test 模式之间切换 baseURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆