zhaobinglong / myBlog

https://zhaobinglong.github.io/myBlog/
MIT License
7 stars 0 forks source link

基于axios的接口封装解决方案 #13

Open zhaobinglong opened 4 years ago

zhaobinglong commented 4 years ago

axios实例化

import axios from 'axios';
const service = axios.create({
  baseURL: 'http://10.122.105.117:3000/mock/14', // url = base url + request url
  timeout: 5000 // request timeout
});

axios请求拦截和加token

service.interceptors.request.use(
  (config) => {
    return config;
    // do something before request is sent
    // if (store.getters.token) {
    //   // let each request carry token
    //   // ['X-Token'] is a custom headers key
    //   // please modify it according to the actual situation
    //   config.headers['X-Token'] = getToken()
    // }
  },
  (error) => { return Promise.reject(error) }
)

和响应拦截

API封装