网上介绍vue设置反向代理的博客很多,方法也很多,我用的是在vue.config.js文件里进行设置,该文件和src文件夹同级
代码如下:
// vue.config.js
const webpack = require('webpack')
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://192.168.20.220:8092',// 发送请求的后台接口
ws: true,
changeOrigin: true,
secure: false,// 如果是https接口,需设置secure为false
pathRewrite: {
'^/api': ''
}
}
}
},
}
如果被访问的接口中含有‘api
’,vue会自动将请求转发到target:''
对应的地址,如果在main.js里面设置了vue的默认路径,使用反向代理时应该把设置默认路径的代码去掉,否则反向代理不起作用。