一、安装引用

安装:

npm install vue-resource --save-dev

引用:

/*引入Vue框架*/
import Vue from 'vue'
/*引入资源请求插件*/
import VueResource from 'vue-resource' /*使用VueResource插件*/
Vue.use(VueResource)

二、简单语法

引入vue-resource后,可以基于全局的Vue对象使用http,也可以基于某个Vue实例使用http

// 基于全局Vue对象使用http
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback); // 在一个Vue实例内使用$http
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
// 传统写法
this.$http.get('/Url', [options]).then(function(response){
// 响应成功回调
}, function(response){
// 响应错误回调
}); // ES6写法
this.$http.get('/Url', [options]).then((response) => {
// 响应成功回调
}, (response) => {
// 响应错误回调
});

1.有7种符合REST风格的请求API:

get(url, [options])
head(url, [options])
delete(url, [options])
jsonp(url, [options])
post(url, [body], [options])
put(url, [body], [options])
patch(url, [body], [options])

2.options对象:

3.emulateHTTP

如果Web服务器无法处理PUT, PATCH和DELETE这种REST风格的请求,你可以启用enulateHTTP现象。启用该选项后,请求会以普通的POST方法发出,并且HTTP头信息的X-HTTP-Method-Override属性会设置为实际的HTTP方法。

Vue.http.options.emulateHTTP = true;

4.emulateJSON

如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type,就像普通的HTML表单一样。

Vue.http.options.emulateJSON = true;

三、实例

var demo = new Vue({
el: '#app',
data: {
gridColumns: ['customerId', 'companyName', 'contactName', 'phone'],
gridData: [],
apiUrl: 'http://127.0.0.1:8080/api/customers'
},
mounted: function() {
this.getCustomers()
},
methods: {
getCustomers: function() {
this.$http.get(this.apiUrl).then((response) => {
this.$set('gridData', response.data)
},(response) => {
console.log("出错了");
}).catch(function(response) {
console.log(response)
})
}
}
});

then方法里提供了successCallbackerrorCallback。catch方法用于捕捉程序的异常,catch方法和errorCallback是不同的,errorCallback只在响应失败时调用,而catch则是在整个请求到响应过程中,只要程序出错了就会被调用。

在then方法的回调函数内,你也可以直接使用this,this仍然是指向Vue实例的。为了减少作用域链的搜索,建议使用一个局部变量来接收this。

四、inteceptor拦截器

拦截器可以在请求发送前和发送请求后做一些处理。在response返回给successCallback或errorCallback之前,你可以修改response中的内容,或做一些处理。 例如,响应的状态码如果是404,你可以显示友好的404界面。再比如我们就用拦截器做了登录处理,所以请求发送之前都要通过拦截器验证当前用户是否登陆,否则提示登录页面。

Vue.http.interceptors.push(function(request, next) {
// ...
// 请求发送前的处理逻辑
// ...
next(function(response) {
// ...
// 请求发送后的处理逻辑
// ...
// 根据请求的状态,response参数会返回给successCallback或errorCallback
return response
})
})

1)为请求添加loading效果

  通过inteceptor,我们可以为所有的请求处理加一个loading:请求发送前显示loading,接收响应后隐藏loading。

//1、添加一个loading组件
<template id='loading-template'>
<div class='loading-overlay'></div>
</template> //2、将loading组件作为另外一个Vue实例的子组件
var help = new Vue({
el: '#help',
data: {
showLoading: false
},
components: {
'loading': {
template: '#loading-template',
}
}
}) //3、将该Vue实例挂载到某个HTML元素
<div id='help'>
<loading v-show='showLoading'></loading>
</div> //4、添加inteceptor
Vue.http.interceptors.push((request, next) => {
loading.show = true;
next((response) => {
loading.show = false;
return response;
});
});

2)为请求添加共同的错误处理方法

//1、继续沿用上面的loading组件,在#help元素下加一个对话框
<div id='help'>
<loading v-show='showLoading' ></loading>
<modal-dialog :show='showDialog'>
<header class='dialog-header' slot='header'>
<h3 class='dialog-title'>Server Error</h3>
</header>
<div class='dialog-body' slot='body'>
<p class='error'>Oops,server has got some errors, error code: {{errorCode}}.</p>
</div>
</modal-dialog>
</div> //2、给help实例的data选项添加两个属性
var help = new Vue({
el: '#help',
data: {
showLoading: false,
showDialog: false,
errorCode: ''
},
components: {
'loading': {
template: '#loading-template',
}
}
}) //3、修改inteceptor
Vue.http.interceptors.push((request, next) => {
help.showLoading = true;
next((response) => {
if(!response.ok){
help.errorCode = response.status;
help.showDialog = true;
}
help.showLoading = false;
return response;
});
});

vue-resourse简单使用方法的更多相关文章

  1. vue.js之生命周期,防止闪烁,计算属性的使用,vue实例简单方法和循环重复数据

    摘要:今天是比较糟糕的一天没怎么学习,原因是学校的wifi连不上了~~.今天学习一下vue的生命周期,如何防止闪烁(也就是用户看得到花括号),计算属性的使用,vue实例简单方法,以及当有重复数据时如何 ...

  2. vue教程2-04 vue实例简单方法

    vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...

  3. vue超简单加载字体方法,解决scss难加载字体的问题

    vue超简单加载字体方法,解决scss难加载字体的问题 scss在加载字体方面一直不太好用,需要繁杂的配置才能达到想要的效果,这里说一种非常简单的方法 在App.vue的style标签下引入字体文件后 ...

  4. react构建淘票票webapp,及react与vue的简单比较。

    前言 前段时间使用vue2.0构建了淘票票页面,并写了一篇相关文章vue2.0构建淘票票webapp,得到了很多童鞋的支持,因此这些天又使用react重构了下这个项目,目的无他,只为了学习和共同进步! ...

  5. 基于Django rest framework 和Vue实现简单的在线教育平台

      一.基于api前端显示课程详细信息 1.调整Course.vue模块 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  6. Vue的简单入门

    Vue的简单入门 一.什么是Vue? vue.js也一个渐进式JavaScript框架,可以独立完成前后端分离式web项目 渐进式:vue可以从小到控制页面中的一个变量后到页面中一块内容再到整个页面, ...

  7. laravel5.4+vue+element简单搭建(gulp+laravel Elixir)(转)

    如今laravel来到5.4版本,更方便引入vue了,具体步骤如下: 下图为我动到的文件 1.下载laravel5.4 2.命令行(laravel5.4目录下):composer install 3. ...

  8. 带二级目录的Nginx配置------目前找到的最简单的方法

    由于项目不知一个,所以不得不为每一个项目建一个专有的文件夹,这就导致了在配置nginx的时候会出现二级目录 目前找到的最简单的方法     - step1:修改 vue.config.js   添加配 ...

  9. MySQL笔记-最简单的方法来解决找不到mysqld.sock文件的问题

    首先,环境:ubuntu 14.04,采用apt-get的方式安装的,手动安装可能路径设置稍有区别. 1.安装MySQL后,用命令行首次启动时发现找不到Mysqld.sock文件,提示: ERROR ...

  10. mfc显示静态图片最简单的方法

    一致都是研究如何调用opencv显示动态图片,但是很多时候在显示图标的时候,都是需要显示静态图片,现在将最简单的方法总结下: 1.添加picture控件 2.添加资源,要求为bmp 3.修改属性 结果 ...

随机推荐

  1. 2. Vim 概念扫盲

    Frm: http://www.linuxidc.com/Linux/2013-05/84031p2.htm 了解Vim的三个基本模式 当我们安装完一个编辑器后,肯定会打开它,然后在里面输入点什么东西 ...

  2. Sigils of Elohim

    题目大意 见游戏链接https://store.steampowered.com/app/321480/. 分析 作为一个程序猿,我拒绝用人脑dfs. 代码如下 #include <bits/s ...

  3. 关于ctype.h头文件使用说明

    ctype.h里的函数概况: 1.字符测试函数 (1)函数原型均为 int isXXX( int ch) (2)参数为int,任何参数均被转换为整形 (3)只能处理[0,127]之间的值 2.字符映射 ...

  4. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  5. Leetcode274.H-IndexH指数

    原题的中文翻译不是很好,所以给出英文版. Given an array of citations (each citation is a non-negative integer) of a rese ...

  6. 笔记23 搭建Spring MVC

    搭建一个最简单的SpringMVC示例 1.配置DispatcherServlet DispatcherServlet是Spring MVC的核心.在这里请求会第一次 接触到框架,它要负责将请求路由到 ...

  7. event.target.tagName

    html中 event.target.tagName 中的tagName属性他返回的就是元素标签的大写名称 例如标签他的tagName就是EM 所以用大写 记住:在 HTML 中,tagName 属性 ...

  8. leetcood学习笔记-79-单词搜索

    题目描述: 方法一;回溯 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: max_x,ma ...

  9. SPR, subpixel rendering

    参考例子:https://www.grc.com/ctwhat.htm https://en.wikipedia.org/wiki/Subpixel_rendering http://archernz ...

  10. 【转载】OpenCL实现矩阵相乘

    矩阵相乘其实就是前一个矩阵的每一行乘以后一个矩阵的每一列,然后将乘后的每一个数字相加,得到结果矩阵的指定位置的数值.具体算法回顾一下线性代数即可.但是这种行列相乘其实都是独立的,如果是CPU计算必须串 ...