首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

Vue回炉重造之三次封装axios

import axios from ‘axios’ // 引入axios import store from ‘…/store/index’ // 引入Vuex import router from ‘…/router’ // 引入vue-router import { Message } from ‘element-ui’ //局部引入UI框架组件 // 环境的切换 if (process.env.NODE_ENV === ‘development’) { axios.defaults.baseURL = ‘https://xxx/’ // 开发环境 } else if (process.env.NODE_ENV === ‘debug’) { axios.defaults.baseURL = ‘’ // 调试环境 } else if (process.env.NODE_ENV === ‘production’) { axios.defaults.baseURL = ‘https://xxx/’ // 生产环境 } axios.defaults.timeout = 10000; // 请求拦截器 axios.interceptors.request.use( config => { if (localStorage.getItem(‘Authorization’)) { config.headers.Authorization = Bearer + " " + localStorage.getItem(‘Authorization’); //查看是否存在token return config; } else if (config.isUpload) { config.headers = { ‘Content-Type’: ‘multipart/form-data’} // 根据参数是否启用form-data方式 return config; } else { config.headers = { ‘Content-Type’: ‘application/json’ } return config; } }, error => { return Promise.error(error) })

03
领券