import axios from "axios"; import NProgress from "nprogress"; import 'nprogress/nprogress.css' const server = axios.create({ baseURL: '/api',//此处 /api 可以写 后端已经处理跨域问题的地址,也可以写 代理跨域 timeout: 5000 }) const request = (url, method='get', data={} ) => { return server({ url, method, data : method === 'post' ? data : null, query : method === 'get' || 'delete' || 'put' ? data : null, }) } server.interceptors.request.use(config => { /** * 在请求体中添加 token */ /* const token = sessionStorage.getItem('token') if(token){ config.headers.token = token } */ NProgress.start(); return config; }, error => { NProgress.done(); return Promise.reject(error); }); server.interceptors.response.use(response => { NProgress.done(); return response.data; }, error => { NProgress.done(); return Promise.reject(error); }); export default{ const get = (url, data) => request(url, 'get', data) const post = (url, data) => request(url, 'post', data) const delete = (url, data) => request(url, 'delete', data) const put = (url, data) => request(url, 'put', data) }
标签:axios,封装,二次,url,request,token,const,data From: https://www.cnblogs.com/ccad/p/16871753.html