axios拦截器token拦截

axios拦截器token拦截

ps:vuex配置请看下一章

/* axios.js */

require('es6-promise').polyfill();
import axios from 'axios';
import store from '@/store';
import router from './router';

import baseURL from './env' // http:// localhost:3000/

const instance = axios.create({
  baseURL,
  timeout: -1
});

instance.interceptors.request.use(function (config) {
  if (store.getters.token) {
    config.headers.Authorization = store.getters.token;
  }else{
    router.push('/'); // 如果没有权限可跳到登录页
  }
  return config;
}, function (error) {

  return Promise.reject(error);
});

instance.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  return Promise.reject(error);
});

export default instance

你可能感兴趣的:(vue,配置)