1.添加代理
vue.config.js
devServer: {
// 配置服务器代理
proxy: {
"/apis": { // 代理接口前缀为/api的请求
"target": 'http://xxxx:3000', // 目标请求地址
"secure": false, // 接受运行在https上,默认不接受
"changeOrigin": true, // 如果设置为true
"pathRewrite": { //重写路径
'^/apis': ''
}
},
// 配置多个代理
"/service": {
"target": 'https://www.google.com/',
"secure": false,
"changeOrigin": true,
},
}
}
2.axios修改路径
const instance = axios.create({
baseURL: '/apis',
timeout: 10000,
withCredentials: true,
})
本文介绍如何在Vue项目中使用vue.config.js文件配置代理服务器,实现不同接口的请求代理,并通过axios实例化配置统一请求路径,提升开发效率。
2568

被折叠的 条评论
为什么被折叠?



