配置两个文件 config/index.js 以及 src/mian.js
config/index.js
首先在config/index.js 里面找到proxyTable :{} ,然后在里面加入
"/api": {
target: 'http://localhost:8080',
changeOrigin: true,
pathRewrite: {
'^/api': '/' // 这里做了一个路径重写
}
}
路径重写说明 :
如上示例最终访问的 url 是; localhost:8080/
改一下 :
pathRewrite: {
'^/api': '/hello' // 这里做了一个路径重写
}
那么最终访问的就是 : localhost:8080/hello/
src/main.js
//引入axios
import axios from 'axios'
//跨域配置
axios.defaults.baseURL="/api"+"/mavenSSM";
//使用了全局配置的axios
Vue.prototype.$axios = axios
// 配置直接使用 axios
window.axios = axios
调用时
let url = "show";
axios({
method: "get",
url: url,
// headers: {
// "Content-type": "application/x-www-form-urlencoded"
// },
// data: TUser
})
.then(response => {
this.msg = response.data
console.log(response);
})
.catch(error => {
this.msg = "error"
console.log(error);
});
控制台显示
是一个服务器转发的过程,url 并不会变