如果 vue-resource 有类似 jQuery.ajaxSetup() 的选项;

if vue-resource has option like jQuery.ajaxSetup();

https://github.com/vuejs/vue-resource

我想将全局设置添加到 Vue.http

像这样

$.ajaxSetup({
    complete: function(xhr, status) {
        if (xhr.status == '401') {
            delCookie('un');
            window.location.hash = '#/login';
        }
    }
});

看起来您应该能够访问和设置 Vue.http.optionsVue.http.headers 中的默认选项。这似乎分别是它们的默认值:

{ // options
    method: 'get',
    params: {},
    data: '',
    xhr: null,
    jsonp: 'callback',
    beforeSend: null,
    crossOrigin: null,
    emulateHTTP: false,
    emulateJSON: false
}

var jsonType = {'Content-Type': 'application/json;charset=utf-8'};
{ // headers
    put: jsonType,
    post: jsonType,
    patch: jsonType,
    delete: jsonType,
    common: {'Accept': 'application/json, text/plain, */*'},
    custom: {'X-Requested-With': 'XMLHttpRequest'}
}

试试这个:

new Vue({
    http: {
        root: '/root',
        headers: {
            Authorization: ''
        }
    }
})

你需要使用拦截器 https://github.com/pagekit/vue-resource/blob/develop/docs/http.md

例如,在你 app.js 或 main.js

Vue.http.interceptors.push((request, next) => {
        next((response) => {
            if (response.status === 403) {
                location.reload();
            }
        });
    });