在使用vue的过程中,跨域封装是避免不了的,也找了一些文章,发现很多都没有解决,这里vue本地跨域封装分享给大家。
1,找到你config文件夹下面的index.js
2,找到dev,在下面修改添加如下代码`
proxyTable: {
'/apishare': {
target: 'https://******.com/',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,
pathRewrite: {
'^/apishare': '' // 这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://localhost:8090/users',直接写‘/api/users’即可
}
}
},
3,在当前组件或者全局引入axios,import axios from ‘axios’
4,请求:
axios({
url:'/apishare/oms/api/dashboard',
method: 'get',
})
.then(res=>{
})
.catch(function(error) {
console.log(error);
});
这样,axios本地跨域封装就可以了