//axios 拦截器const publicRequests = {Get(url, params = {}) {return new Promise((resolve, reject) => {axios.get(Vue.prototype.http + url, { params }).then(res => {resolve(res);}).catch((err) => {reject(err);});});},Post(url, params = {}) {const instance = axios.create({headers: { \'token\': Storage.getJson(\'user\').token }});instance.interceptors.response.use(config => {if (config.data.code == \'096\') {Vue.prototype.$error(\'请重新登录\')Storage.remove(\'user\')router.push({ name: \"login\" })}return config}, function (err) {return Promise.reject(err)})return new Promise((resolve, reject) => {instance.post(Vue.prototype.http + url, params).then(res => {resolve(res);}).catch(err => {console.log(err)reject(err);});});},fetchPost(url, params = {}) {return new Promise((resolve, reject) => {axios.post(Vue.prototype.http + url, params).then(res => {resolve(res);}).catch(err => {reject(err);});});},fetchFilePost(url, params) {const instance = axios.create({headers: { \'Content-Type\': \'multipart/form-data\' }});return new Promise((resolve, reject) => {instance.post(HTTP + url, params).then(res => {resolve(res);}).catch(err => {reject(err);});});}};
axios拦截器,封装使用
未经允许不得转载:爱站程序员基地 » axios拦截器,封装使用