1.安装cross-env
npm install --save-dev cross-env
2.根目录新建env.js
module.exports={
dev:{
MODE:'devlopment',
ENV_API:'http://xxx/',
OTHER_URL:"xxx"
},
test:{
MODE:'test',
ENV_API:'http://xxx',
OTHER_URL:"xxx"
},
pro:{
MODE:'production',
ENV_API:'http://xxx',
OTHER_URL:"xxx"
}
}
3.nuxt.config.js
env:{
OTHER_URL:env[process.env.MODE].OTHER_URL,
},
proxy: {
'/api': {
target: env[process.env.MODE].ENV_API,
pathRewrite: {
changeOrigin: true,
'^/api' : ''
}
}
},
4.配置package.json
"scripts": {
"dev": "cross-env MODE=dev nuxt",
"pro": "cross-env MODE=pro nuxt",
"build:test": "cross-env MODE=test nuxt build",
"build": "cross-env MODE=pro nuxt build",
"start:test": "cross-env MODE=test nuxt start",
"start": "cross-env MODE=pro nuxt start",
"generate": "nuxt generate"
},
5.测试环境部署
npm run build:test
npm run start:test
生产环境部署
npm run build
npm run start