1、开发环境下:
通过vue_cli脚手架生成的vue项目,有时会生成config文件夹,里面会有个index.js文件,如果没有我们可以再项目根目录下新建vue.config.js文件,内容设置如下:
module.exports = {
devServer: {
proxy: { //配置跨域
'/api' : {
target: 'http://10.0.0.105:5590', //这里后台的地址应该填写你们真实的后台接口
changOrigin: true, //允许跨域
pathRewrite: {
/* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
实际上访问的地址是:http://10.0.0.105:5590/core/getData/userInfo,因为重写了 /api
*/
'^/api': ''
}
},
}
},
}
2、部署环境下:
通过npm run build打包后会生成dist文件夹,把dist文件夹下所有内容复制到nginx-1.21.3\html 路径下,然后修改nginx-1.21.3\conf\下的nginx.conf,增加如下内容,然后重启服务:
server