vue-resource.js小记

npm install vue-resource

引入后Vue可以基于全局前对象Vue使用http,也可以基于某个Vue实例使用http:

//基于全局Vue对象使用http
Vue.http.get('/url',[options]).then(successCallback,errorCallback);
Vue.http.post('/url',[body],[options]).then(successCallback,errorCallback);

//在一个Vue实例内使用$http
this.$http.get('/url',[options]).then(successCallback,errorCallback);
this.$http.post('/url',[body],[options]).then(successCallback,errorCallback);

发送请求之后,使用then方法来处理响应结果,第一个参数成功时的回调函数,第二个参数失败时的回调函数。

then的回调函数有两种写法,第一种是传统写法,第二种是更为简洁的ES6的lambda写法:

//传统写法
this.$http.get('/url',[options]).then(function(response){
    //响应成功
},function(response){
    //响应失败
});

//lambda写法
this.$http.get('/url',[options]).then((response)=>{
    //响应成功
},
(response)=>{
    //响应失败
});

vue-resource的请求API是按照REST风格设计的,他提供了7种请求API:

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

出了jsonp以外,;另外6种的API名称是标准的HTTP方法。

options对象包含的属性

参数类型描述
urlstring请求的url
methodstring请求的HTTP方法,'GET'、'POST'或其他
body

object,FormData,string

request body
paramsobject

请求的url参数对象

headersobjectrequest header
timeoutnumber单位为毫秒的请求超时时间(0表示无超时时间)
beforefunction(request)请求发送前的处理函数,类似jquery的beforeSend函数
progressfunction(event)progressEvent回调处理函数
credentialsboolean表示跨域请求时是否需要使用凭证
emulateHTTPboolean发送PUT,PATCH,DELETE请求时以HTTP POST的方式发送,并设置请求头X-HTTP-Method-Override
emulateJSONboolean将request body以application/x-www-form-urlencoded content type 发送

response对象

注:v0.9.0以前的版本没有text(),json(),blob()这些方法

方法类型描述
text()string以string的形式返回response body
json()object以JSON对象的形式返回response body
blob()Blob以二进制形式返回response body
属性类型描述
okboolean响应的HTTP状态在200~299之间,该属性为true
statusnumber

响应的HTTP状态码

status textstring响应的状态文本
headsobject响应头

使用resource服务

vue-resource提供了另一种方式访问HTTP--resource服务,resource服务包含以下几种默认的action:

  • get: {method: 'GET'}
  • save: {method: 'POST'}
  • query: {method: 'GET'}
  • update: {method: 'PUT'}
  • remove: {method: 'DELETE'}
  • delete: {method: 'DELETE'}

resource对象也有两种访问形式

  • 全局访问:Vue.resource
  • 实例访问:this.$resource

resource可以结合URI Template一起使用

apiUrl:'http://localhost/api/customers{/id}'


//eg 使用id
updateCustomer: function() {
    var resource = this.$resource(this.apiUrl)
        vm = this
        
    resource.update({ id: vm.item.customerId}, vm.item)
        .then((response) => {
            vm.getCustomers()
        })
}

//未使用id
updateCustomer: function() {
    var resource = this.$resource(this.apiUrl)
        vm = this
        
    resource.update(vm.apiUrl, vm.item)
        .then((response) => {
            vm.getCustomers()
        })
}

使用interceptor

基本用法

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

总结

vue-resource是一个非常轻量的用于处理HTTP请求的插件,它提供了两种方式来处理HTTP请求:

  • 使用Vue.http或this.$http
  • 使用Vue.resource或this.$resource

两种方式本质上没什么区别,阅读vue-resource的源码,你可以发现弟2中方式是基于第一种方式实现的。

interceptor可以在请求前和请求后附加一些行为,这意味着除了请求处理的过程,请求的其他环节都可以由我们来控制。

参考链接:

https://github.com/pagekit/vue-resource/tree/master/docs

转载于:https://my.oschina.net/zx204721/blog/1057837

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值