1.安装axios
npm install --save axios
2.引用axios
在main.js中引用axios
import axios from 'axios'
Vue.prototype.$axios = axios //修改原始数据
axios.defaults.baseURL='/api'
3.设置代理
在vue.config.js中,设置代理
module.exports = { devServer: { open: true, //是否自动弹出浏览器页面 host: "localhost", port: '8081', https: false, hotOnly: false, proxy: { '/api': { target: 'ip地址/api', //API服务器的地址 changeOrigin: true, pathRewrite: { '^/api': '' } } }, } }
接下来就可以调用了
methods:{
dopost(){
this.$axios.post('接口地址',{},{headers:{'accessToken':''}}
).then(response=>{
if(response.data){
this.tableData=response.data.data;
}
}).catch(err=>{
alert('请求失败')
})
}
},mounted(){
this.dopost();
}