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方法
- 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
- 只是在简单使用上面做了总结 后续继续完善
- 20170629 补充了handleErrors部分 来自github官网 有时间贴demo
1 0
- vue-resource和axios的简单使用方法总结
- Vue基础知识之vue-resource和axios
- vue中axios的使用方法
- Vue学习基础之vue-resource和vue axios
- vue-resource+jquery+axios请求
- 简单的springMVC+vue.js+axios实现
- vue axios 的使用
- vue 插件和axios封装
- vue.js axios 的使用
- vue 中axios的封装
- vue vuex vue-router vue-resource 简单的搭建一个 vue 小项目
- vue vuex vue-router vue-resource 简单的搭建一个 vue 小项目
- vue axios
- Vue axios
- vue axios 遇到“No 'Access-Control-Allow-Origin' header is present on the requested resource.”的问题
- Vue的axios跨域问题
- Vue axios中this的指向问题
- vue+axios自己踩过的坑
- toj-2849-Fence(优先队列)
- 竞态与并发
- 用IDEA开发BigDL——Scala
- Problem D: 梯田AGAIN
- odoo报错 load could not load template Template: website.layout
- vue-resource和axios的简单使用方法总结
- 高级IO操作
- 多线程编程(一)--进程和线程之由来
- TSNE动态可视化
- JVM 调优实战之优化 eclipse 启动时间
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- toj-3488-stones2(优先队列)
- 如何去硅谷就业?
- 回溯法解决N皇后问题(java实现)