首先下载axios
npm i axios
在main.js中全局引用:
import axios from 'axios'
Vue.prototype.$axios = axios
在vue根目录下创建 vue.config.js 文件(此处文件名必须)
vue.config.js填入代码:
module.exports = {
devServer: {
proxy: {
'/api': {
target: "http://118.123.249.152:36471", //服务器ip地址及端口号
changeOrigin: true, //允许跨域
pathRewrite: {
'^/api': ''
}
}
}
}
}
使用:
可以在main.js给axios设置baseUrl:
axios.defaults.baseURL = '/api'
也可以在axios 请求url前面加 /api
示例:
this.$axios
.get("/api/v1/GetDeviceInfo", { params })
.then((response)=> {
console.log(response);
})
.catch((error)=> {
console.log(error);
});
完成!