Angular2 How to switch baseURL between prodmode and test mode

时光毁灭记忆、已成空白 提交于 2019-12-11 04:29:45

问题


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)

How can I do this?

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


回答1:


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"
}

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

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

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 automatically use environment.yourCustomEnv.ts file at every place where you import environment.

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



来源:https://stackoverflow.com/questions/43136109/angular2-how-to-switch-baseurl-between-prodmode-and-test-mode

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