During the development process, the address called by the interface in the npm run dev component is an API variable defined by global global as follows:
//Open the development environment
// global.API = '/api';
//Open the test environment
// global.API = '/test';
//Open uat environment
// global.API = '/uat';
//Open the production environment
// global.API = '/www';
Production environment How to automatically change API to production environment variables according to different interfaces when using npm run build?
After pondering over the code generated by vue-cli for a few days, Vue thinks you only need three configurations: development (npm run dev), production (npm run build) and unit testing (npm run test). . .
End of rant.
1. Open
config/index.js
and modify theenv
file name inbuild
to obtain it based on the environment variable.After modification, it will probably look like this:
2. Copy
prod.env.js
into two copies in the config directory, namelyuat.env.js
andtesting.env.js
3. Modify the four files
dev.env.js
,uat.env.js
,testing.env.js
,prod.env.js
, and addAPI: '" in the braces /api or uat or something else"'
, as follows:4. Then you can access this variable through
process.env.API
anywhere.5. When packaging, first set the environment variable
VUE_CONFIG
, for exampleexport VUE_CONFIG=uat && npm run build
.https://stackoverflow.com/que...
https://github.com/tonyljl526...
You can take a look at the scaffolding of this project, which is based on express, vue2, and webpack. To achieve your needs, mainly look at config/renderConfig.js