1、在项目根目录新建文件vue.config.js(文件名必须是这个)
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8866/', //对应自己想要访问的另一个域的接口
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
2、组件中使用axios发起数据请求,使用/api
created(){
this.$axios({
url:"/api/getData.do",
method:"post"
}).then(resp=>{
this.stuArr=resp.data.data
console.log(resp.data.data);
}).catch(err=>{
console.log(err);
})
}
3、另一个域中的router可直接拦截/getData.do,返回响应数据
4、运行两个域的服务器,访问页面