Vue.js本身沒有提供與服務(wù)端通信的借口疾宏,但是通過插件的形式實(shí)現(xiàn)了基于Ajax、Jsonp等技術(shù)的服務(wù)端通信酝静。
ps:說明下骄蝇,從vue2.x開始尤大大推薦使用axios進(jìn)行數(shù)據(jù)交互,關(guān)于axios后續(xù)將繼續(xù)和大家一起學(xué)習(xí)谷醉。
今天我們就一起學(xué)習(xí)一下vue-resource致稀。
一、安裝
npm install vue-resource --save
//然后在main.js(入口文件)中引入并注冊
import Vue from 'vue';
import VueResource from 'vue-resource'
Vue.use(VueResource)
二俱尼、參數(shù)配置
vue-resource將請(qǐng)求配置分為全局配置抖单、組件實(shí)例配置和調(diào)用配置這三部分。并且這三部分的優(yōu)先級(jí)依次增高遇八。
1. 全局配置
使用全局配置設(shè)置默認(rèn)值矛绘。
Vue.http.options.root = '/root';
2.組件實(shí)例配置
在組件實(shí)例化的配置參數(shù)http選項(xiàng)中進(jìn)行配置。
new Vue({
http:{
root: './root',
headers{
Authorization: 'Basic datura'
}
}
})
3.方法調(diào)用時(shí)配置
new Vue({
methods:{
getData(){
this.$http.get({
url: '/api',
headers: {
Authorization: 'Basic datura'
}
}).then(function(res){
//請(qǐng)求成功的回調(diào)
},function(err){
//請(qǐng)求失敗的回調(diào)
})
}
}
})
三刃永、具體調(diào)用(請(qǐng)求數(shù)據(jù))
1.方式一(底層式)
new Vue({
methods:{
getData(){
//POST請(qǐng)求货矮,帶參數(shù)
this.$http({
url: '/api',
method: 'POST',
data:{
'username': 'datura_lj',
'password': 123456
},
headers: {
'Content-Type': 'x-wwww-form-urlencoded'
}
}).then(function(res){
//請(qǐng)求成功的回調(diào)
},function(err){
//請(qǐng)求失敗的回調(diào)
})
}
}
})
2.方式二(便捷式)
不同于底層式方法,便捷式方法是其實(shí)是對(duì)底層方法進(jìn)行了封裝揽碘,它提供了7種請(qǐng)求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])
參數(shù)說明:
參數(shù) | 數(shù)據(jù)類型 | 說明 |
---|---|---|
url | string | 請(qǐng)求數(shù)據(jù)的地址 |
body | Obj, FormData, string | request body |
headers | Obj | request header |
params | Object | 請(qǐng)求的URL參數(shù)對(duì)象 |
method | string | 請(qǐng)求的HTTP方法次屠,例如:'GET', 'POST'或其他 |
timeout | number | 單位為毫秒請(qǐng)求超時(shí)時(shí)間 (0 表示無超時(shí)時(shí)間) |
before | function(request) | 請(qǐng)求發(fā)送前的處理函數(shù),類似于jQuery的beforeSend函數(shù) |
progress | function(event) | ProgressEvent回調(diào)處理函數(shù) |
credentials | boolean | 表示跨域請(qǐng)求時(shí)是否需要使用憑證 |
emulateHTTP | boolean | 發(fā)送PUT, PATCH, DELETE請(qǐng)求時(shí)以HTTP POST的方式發(fā)送雳刺,并設(shè)置請(qǐng)求頭的X-HTTP-Method-Override |
emulateJSON | boolean | 將request body以application/x-www-form-urlencoded content type發(fā)送 |
四劫灶、實(shí)戰(zhàn)代碼
// 基于全局Vue對(duì)象使用http
Vue.http.get('/api', [options]).then(successCallback, errorCallback);
Vue.http.post('/api', [body], [options]).then(successCallback, errorCallback);
// 在一個(gè)Vue實(shí)例內(nèi)使用$http
this.$http.get('/api', [options]).then(successCallback, errorCallback);
this.$http.post('/api', [body], [options]).then(successCallback, errorCallback);
methods:{
get1:function(){
//發(fā)送get請(qǐng)求
this.$http.get('/api').then(function(res){
//請(qǐng)求成功函數(shù)
},function(){
//請(qǐng)求失敗函數(shù)
});
},
get2:function(){
//發(fā)送get請(qǐng)求
this.$http.get('get.do',
{
a:1,
b:2
}
).then(function(res){
//請(qǐng)求成功函數(shù)
},function(){
//請(qǐng)求失敗函數(shù)
});
},
post:function(){
//發(fā)送post請(qǐng)求
this.$http.post('post.do',
{
a:1,
b:2
}
).then(function(res){
//請(qǐng)求成功函數(shù)
},function(){
//請(qǐng)求失敗函數(shù)
});
},
put:function(){
this.$http.put('put.do',{
"id": 111,
"hascollect": 1
}).then(function(res){
//請(qǐng)求成功函數(shù)
}
},function(err){
//請(qǐng)求失敗函數(shù)
});
}
}