在项目根目录上新建一个vue.config.js的文件
module.exports = {
devServer: {
proxy: {
'/api': {
// 目标网址
target: 'https://www.baidu.com/', // 填写你自己需要访问的网址
// 允许
changOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
挂载: 在main.js中挂载
import axios from 'axios' // 导入 axios
axios.defaults.baseURL = '/api' // 设置请求头中设置/api实现跨域
Vue.prototype.$http = axios // 将axios挂载到全局对象上
使用:
this.$http.post('login').then(res =>{
console.log(res);
})