配置了proxyTable后仍然没有作用,可能原因是baseURL的问题。
解决:把baseURL设置注释掉重启服务。
如下配置:
// vue.config.js
module.exports = {
// ...
devServer: {
proxy: {
'/api': {
target: 'http://backend.server.com', // 后端服务器地址
changeOrigin: true, // 改变源地址
pathRewrite: {
'^/api': '' // 重写路径
}
}
}
}
};
假如你的请求URL是这样的:http://localhost:8080/api/some-endpoint,Vue CLI会自动将/api替换为target中定义的地址,并添加pathRewrite定义的路径。
其中,匹配是以API地址前缀为准,假如你的请求地址是/test/api/some-endpoint,配置代理时仅以/api去匹配是不成功的,会报404错误,正确配置应该是/test/api作为前缀。