2.x
打开config/index.js
在proxyTable
中添写如下代码:
proxyTable: {
'/api': { //使用 "/api" 来代替 "http://xxx"
target: 'http://www.baidu.com',//源地址
changeOrigin: true, //改变源
pathRewrite: {
'^/api': '' //路径重写
//'^/api': 'http://www.csdn.net' //也可以是其他地址 //路径重写
}
}
}
附请求:
getData () {
axios.get('/api/xxx', function (res) {
console.log(res)
})
3.x
- 在
根目录
下建立一个vue.config.js
的文件(3.x 版本脚手架)
- 书写代码
module.exports = {
//---------------------------------------------- 配置 vue 自己的参数
devServer:{
open:true,// 自动开启
port:80, // 设置端口号
host:"127.0.0.1"// 设置 host
//----------------------------------------------设置开发接口
proxy: {
'/api/': {
target: 'http://localhost:3001', // 代理地址
changeOrigin: true,
pathRewrite: {
'^/api': '' //重写地址
},
},
}
}
}
附请求:
getData () {
axios.get('/api/xxx', function (res) {
console.log(res)
})