vue项目中的request.js

vue项目中的请求发送,自己创建一个axios实例

request.js

import axios from 'axios';
import store from "../store/index"
import baseURL from '@/src/api/baseUrl'


const service = axios.create({
  //正式
  baseURL,
  timeout: 5000 * 10
});



service.interceptors.request.use(
  config => {
    config.headers.Authorization = store.state.token  //请求头加上token
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return response.data;
    } else {
      return Promise.reject();
    }
  },
  error => {
    return Promise.reject(error.response);
  }
);

export default service;

上一篇:LeetCode刷题记148-126. 单词接龙 II


下一篇:二次封装axios