基于promise用于浏览器和node.js的http客户端
安装
$ npm install axios
发起一个GET
请求
axios.get("/api/index.json").then(this.getHomeInfoSucc);
data() {
return {
city: "",
swiperList: [],
iconList: [],
recommendList: [],
weekendList: []
};
},
methods: {
getHomeInfo() {
axios.get("/api/index.json").then(this.getHomeInfoSucc);
},
getHomeInfoSucc(res) {
res = res.data;
if (res.ret && res.data) {
const data = res.data;
this.city = data.city;
this.swiperList = data.swiperList;
this.iconList = data.iconList;
this.recommendList = data.recommendList;
this.weekendList = data.weekendList;
}
console.log(res);
}
},
请求方法别名
为了方便我们为所有支持的请求方法均提供了别名。
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])