背景: 解决思路:
let api = ""
let loginServer = "/loginServer"
let businessServer = "/businessServe"
if(process.env.NODE_ENV == "development"){
api = "/api"
loginServer = api + LoginServer
businessServer = api + businessServer
}
export {
loginServer,
businessServer
}
import {loginServer} from 'constants'
function login(params){
return axios.post(loginServer+"/login",params)
}
在vue.config.js中配置代理
modules.exports = {
publicPath:"/" ,
devServer: {
port: 8080,
proxy:{
'/api/loginServer':{
target:"http://localhost:8080",
ws:true,
changeOrigin:true,
pathRewrite:{
'^/api':'/'
}
},
'/api/businessServer':{
target:"http://localhost:8081",
ws:true,
changeOrigin:true,
pathRewrite:{
'^/api':'/'
}
}
}
}
}
解决方案:
script:{
"serve":"vue-cli-service serve --mode=dev"
}
)
loginServerURL =http://localhost:8080
businessServerURL = http://localhost:8081
.env.prod.local等
modules.exports = {
publicPath:"/" ,
devServer: {
port: 8080,
proxy:{
'/api/loginServer':{
target:process.env.loginServerURL?process.env.loginServerURL:"httpL//localhost:8080",
ws:true,
changeOrigin:true,
pathRewrite:{
'^/api':'/'
}
},
'/api/businessServer':{
target:process.env.loginServerURL?process.env.businessServerURL :"http://localhost:8081",
ws:true,
changeOrigin:true,
pathRewrite:{
'^/api':'/'
}
}
}
}
}总结
以上所述是小编给大家介绍的Vue多环境代理配置方法思路详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自学php网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!