赞
踩
import Vue from 'vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
<template lang="html">
</template>
<script>
export default {
data(){
return{
}
},
methods:{
loadData(){
this.$http.get('/url').then((response) => {
// success
console.log(response.body.data);
}),(error => {
// error
console.log(error);
})
}
}
}
</script>
<style lang="css">
</style>

- Vue.prototype方法
import Vue from 'vue';
import axios from 'axios';
Vue.prototype.$axios = axios;
<template lang="html">
</template>
<script>
export default {
data(){
return{
}
},
methods:{
loadData(){
this.$axios.get(['/static/data/user.json'])
.then((response) => {
// success
console.log(response.data);
})
.catch((error) => {
//error
console.log(error);
})
}
}
}
</script>
<style lang="css">
</style>

axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status); // 状态码
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});

axios.get('/user/12345', {
validateStatus: function (status) {
return status < 500; // Reject only if the status code is greater than or equal to 500
}
})
- 只是在简单使用上面做了总结 后续继续完善
- 20170629 补充了handleErrors部分 来自github官网 有时间贴demo
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。