1、安装
cnpm install axios --save-dev
2、注册到vue中,main.js中
import axios from 'axios'
Vue.prototype.$axios = axios // 将axios添加到原型
3、跨域解决方法,在config/index.js中,dev下的
proxyTable: {
'/api': {
target: 'http://localhost:56678', // 设置api == http...
changeOrigin: true,
pathRewrite: {
'^/api': '' // 重写,以api开头的都把挨批变为target,即http../index == api/index
}
}
},
4、使用:这里的/api/shop/index == http://localhost:56678/shop/index
created() { // 组件实例化完成,还未挂载
this.$axios.get("/api/shop/index")
.then(res => {
this.seller = res.data;
})
.catch(err => {
console.log(err);
})
}