Axios為vue2.0官方推薦HTTP請求工具,之前的是vue-resource
在使用的過程中總結了兩種使用方式:
1.和vue-resource使用類似
引入:import axios from 'axios';
Vue.prototype.$http = axios;
使用:this.$http.get(URL).then(response => { // success callback }, response => { // error callback });
2.在你需要的組件中使用
引入:import axios from 'axios';
使用:axios
.get(URL).then(response => { // success callback }, response => { // error callback });
注:在將response返回值賦值給data是一直不成功,之前的代碼:
axios.get()
.then(function (response) {
if (response.data.errno === ERR_OK) {
this.seller = response.data.data;
}
console.log(response.data.data);
})
.catch(function (error) {
console.log(error);
});
現在改為es6語法規范就好了,現在的代碼:
axios.get('/api/seller').then(response => {
if (response.data.errno === ERR_OK) {
this.seller = response.data.data;
}
}, response => {
console.log(response);
});