提示:vue-cli3
通过proxy:
提示:修改配置需要重启
. 通过vue ui创建的项目可以在创建时就选择下载axios,之后就可以直接使用
. 下载axios `npm install axios`
在main.js中
//导入
import axios from 'axios'
//全局挂载
Vue.prototype.$axios = axios
//配置请求根路径
axios.defaults.baseURL = '/api' //自定义接口
*******************************************************
在创建的vue.config.js文件中(和package.json同级)
module.exports={
devServer:{
proxy:{
'/api': {
target: '放用来替换的目的地址',
changeOrigin:true, //是否替换
pathRewrite: { //路径重写
'^/api': ''
}
}
}
}
}
*******************************************************
在App.vue中
this.$axios.get('/').then(response=>{
if(response.data){
console.log("成功")
}
}).catch(err=>{
alert("失败")
})
实现其他路径时前面加上/api就行