axiso 封装

import axios from 'axios'
import { getToten } from './auth'

const instance = axios.create({
    baseURL: 'http://xxxxx:8080',
    timeout: 5000
})

// 拦截器 - 请求拦截
instance.interceptors.request.use(config=>{
    let token = localStorage.getItem('token');
    if(token){
        config.headers = {
            'Authorization': getToten
        }
    }
    return config;
}, err=>{
    return Promise.reject(err)
})


// 拦截器 - 响应拦截
instance.interceptors.response.use(res=>{
    return res;
}, err=>{
    return Promise.reject(err)
})

export default instance

使用

import request from '@/utils/request'

export function getData({ num, type }) {
  return request({
    url: `地址`,
    method: 'post',
    data: { num, type }
  })
}

你可能感兴趣的:(知识在于积累,前端)