vue-cli 多环境切换

文件格式:
.env # 所有环境
.env.local # 所有环境,git忽略
.env.[mode] # mode环境
.env.[mode].local # mode环境,git忽略
build:
// 指定 .env.[mode]
"scripts": {
"build": "vue-cli-service build --mode [mode]"
}
npm run build
.env.[mode] 配置
NODE_ENV = development
VUE_APP_OUT = ./dist/dev
VUE_APP_BASE_URL = http://localhost
VUE_APP_PORT = 4001
- NODE_ENV 有三个取值:development 、production 、test
- BASE_URL 就是 vue.config.js 里的 publicPath
- VUE_APP_* 是特殊值,可以理解为全局变量,可在其他js中通过 process.env.VUE_APP_* 引用
vue.config.js
const baseURL = process.env.VUE_APP_BASE_URL
const port = process.env.VUE_APP_PORT
const targetServer = baseURL + ":" + port
module.exports = {
devServer: {
host: "localhost",
port: 11016,
proxy: {
"server": {
target: targetServer,
changeOrigin: true,
pathRewrite: {
'^/server': ''
}
}
}
},
publicPath: './',
outputDir: process.env.VUE_APP_OUT
}

浙公网安备 33010602011771号