一、安裝引用
安裝:
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方法里提供了successCallback,errorCallback。catch方法用於捕捉程序的異常,catch方法和errorCallback是不同的,errorCallback只在響應失敗時調用,而catch則是在整個請求到響應過程中,只要程序出錯了就會被調用。
四、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; }); });
