vue.config.js 是一个可选的配置文件,如果项目的 (和 package.json 同级的) 根目录中存在这个文件,那么它会被 @vue/cli-service 自动加载。
以下为代理到某个服务的最小化代码配置,以解决前端跨域问题
module.exports= {
devServer: {
proxy: {
'/baidu': {
target:'https://www.baidu.com/',
changeOrigin:true,
pathRewrite:{
'^/baidu':'/'
}
}
}
}
}
纯静态的前端站点有非常强的缓存,要特别注意
在服务端为每一个请求都加上跨域的头(非必须)
module.exports= {
devServer: {
proxy: {
'/baidu': {
target:'https://www.baidu.com/',
changeOrigin:true,
pathRewrite:{
'^/baidu':'/'
}
,header:{
'Access-Control-Allow-Methods':'POST,GET,OPTIONS,DELETE',
'Access-Control-Allow-Headers':'Authorization'
}
}
}
}
}
最详细的代理配置参考官方文档
https://github.com/chimurai/http-proxy-middleware#proxycontext-config