在main.js中
import axios from ‘axios’ //引入axios
Vue.prototype.$http = axios //配置
//在之前登陆的时候已经拿到token并且存入本地
axios.interceptors.request.use(function (config) {
// 这里的config包含每次请求的内容
let token = window.sessionStorage.getItem(‘token’)
if (token) {
// 添加headers
config.headers.Authorization = ${token}
;
config.headers[‘content-type’] = ‘application/x-www-form-urlencoded;charset=UTF-8’;
} else {}
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
//还可以在方法里添加全局的加载动画