axios 拦截

vue axios拦截

import Vue from 'vue'
import axios from 'axios'

// http request 拦截器
axios.interceptors.request.use(
  config => {
    // 从本地存储获取token
    var token = window.localStorage.token
    if (token) {
      config.headers.Authorization = `token ${token}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });

// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
    }
    return Promise.reject(error.response.data)
  });

你可能感兴趣的:(axios 拦截)