1.首先找到这个文件,可能脚手架版本不一样,但是这个文件是一定有的
2.然后在这个叫做dev的对象里面插入proxyTable这个属性
例:
proxyTable: {
'/api': {
target: 'http://xxx.xxxx.xx/', //设置调用的接口域名和端口号
changeOrigin: true,//如果需要跨域
ws:true,
pathRewrite:{
'^/api':''
}
}
}
3.这样你就可以在main.js文件配置默认url前缀的时候进行添加了
例:
// // 设置axios默认url前缀
if (location.href.indexOf('http://localhost:8080') !== -1) { // 本地时
axios.defaults.baseURL = '/api'
} else if (location.href.indexOf('https') !== -1) { // https协议时
axios.defaults.baseURL = 'https://xxx.xxx.xx'
} else { // http协议时
axios.defaults.baseURL = 'http://xxx.xxx.xx'
}
4.然后你在本地开发时用axios请求这个(http://xxx/xxx/xx)的接口是就不会跨域了
例:
this.$axios({
url: '/aa/aa',
method: 'post'
}).then(res => {
console.log(res)
}
就相当于访问http://localhost:8080/api/aa/aa ==> http://xxx/xxx/xx/aa/aa这个接口了