AI智能
改变未来

axios+element配置请求等待动画、请求异常弹出消息;

_axios.interceptors.request.use(function(config) {// Do something before request is sentif (config.method === \'post\' && Object.prototype.toString.call(config.data) !== \'[object FormData]\') {config.data = qs.stringify(config.data); // post请求格式化数据config.headers[\'formdata\'] = \'1\';}loadingInstance = Loading.service({ // 加载动画text: \'加载中\',background: \'rgba(255, 255, 255, 0.6)\'});loadingArr++;return config;},function(error) {// Do something with request errorreturn Promise.reject(error);});_axios.interceptors.response.use(function(response) {// Do something with response dataloadingArr--; // 动画加载个数减1if (loadingArr === 0) {loadingInstance.close();}return response.data;},function(error) {// Do something with response errorloadingArr--; // 动画加载个数减1if (loadingArr === 0) {loadingInstance.close();}if (error && error.response) {switch (error.response.status) {case 400:error.message = \'错误请求\';break;case 404:error.message = \'请求错误,未找到该资源\';break;case 405:error.message = \'请求方法未允许\';break;case 500:error.message = \'服务端出错\';break;default:error.message = `连接错误${error.response.status}`;}} else {error.message = \'连接服务器失败\';}Message.warning(error.message);return Promise.reject(error.message);});
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » axios+element配置请求等待动画、请求异常弹出消息;