vue-resource和axios的简单使用方法总结

来源:互联网 发布:淘宝怎么过高 编辑:程序博客网 时间:2024/06/14 03:33

vue-resource和vue-axios的简单使用方法


vue-resource的使用

首先要在main.js中引入

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-axios的使用

首先要在main.js中引入 But axios不能使用use方法

  1. 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>

vue-axios的错误处理

why? 实现网络不可达,网络无响应等等情况下的处理机制

Handling Errors

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);    });    

You can define a custom HTTP status code error range using the validateStatus config option. –自定义选项

axios.get('/user/12345', {    validateStatus: function (status) {        return status < 500; // Reject only if the status code is greater than or equal to 500    }})

Summary

  1. 只是在简单使用上面做了总结 后续继续完善
  2. 20170629 补充了handleErrors部分 来自github官网 有时间贴demo
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 DNF龙腾光环领错角色怎么办 海盗来了卡在新手奖励怎么办 海盗来了获得新手奖励卡了怎么办 魔域帐号忘了哪个区的了怎么办 魔域口袋版幻兽栏满了怎么办 苹果平板的密码和邮箱都忘了怎么办 二级报名忘了邮箱和密码怎么办 用身份证买的手机号变成空号怎么办 魔域口袋版背包幻兽满了怎么办? 魔域宝宝转世满了还不变雷怎么办 魔域口袋版家族试炼士气不足怎么办 聚水潭暂存位没有箱号怎么办? 长时间不锻炼运动后浑身酸痛怎么办 体脂称身体年龄比实际年龄大怎么办 分手两天我好难过好想去找他怎么办 宝宝一吃药就哭得很厉害怎么办 比你入职晚的同事比你厉害怎么办 nba篮球大师能力值满了怎么办 做什么都觉得很累没有兴趣怎么办 剧烈运动后头晕恶心想吐怎么办 剧烈运动完头晕恶心想吐怎么办 小孩头部摔到地上想吐头晕怎么办 肺部化疗恶心反胃怎么办吃什么药好 好久没运动一运动全身酸痛怎么办 怀孕两个月了一直吐没体力怎么办 公务员审查六级口语证丢了怎么办 农资经营如果被执法查处了怎么办? 干隧道测量坚持不下去该怎么办 老家房子强拆没钱做没钱补怎么办? 魔兽世界出了两个一样的传说怎么办 死亡事故后遇上不讲理家属怎么办 死亡事故后死亡者家属不讲理怎么办 住的地方治安乱混混多怎么办 使命召唤进去感觉动作好慢怎么办 40岁后不够硬不持久怎么办 如果别人毁灭了你的梦想怎么办 如果死者托梦说自己过的不好怎么办 吵架了老婆跑回娘家不回家了怎么办 和老婆吵架她跑回娘家了怎么办 鞋子里面的皮穿的都是泥怎么办 绝地求生手游模拟器键位不灵怎么办