The steps to configure different packaging commands for different environments in vue are as follows:
- Create a new file in the root directory of the project
.env
to store public environment variables. - Create new files such as .env.development, .env.test, and .env.production in the root directory of the project to store variables in different environments, such as API addresses and port numbers.
- Add packaging commands for different environments in the scripts field in the package.json file,
for example"build:test":“vue-cli-service build --mode test”
, where --mode specifies which .env file to use. Use in the projectprocess.env
to obtain the corresponding environment variables.
Example: For example, to obtain the environment variables of the test environment
My .env.test
file content is as follows:
NODE_ENV=test // 环境区分变量
VUE_APP_MODE =test2 // 其他变量
Then vue.config.js
get the environment NODE_ENV
method in:process.env.NODE_ENV