解决vue 使用axios.all()方法发起多个请求控制台报错的问题
今天在项目中使用axios时发现axios.all() 方法可以执行但是控制台报错,后来在论坛中看到是由于axios.all() 方法并没有挂载到 axios对象上,需要我们手动去添加

== 只需要在你封装的axios文件里加入 ==
instance.all = axios.all
就完美解决了!
补充知识:vue项目中使用axios.all处理并发请求报_util2.default.axios.all is not a function异常
报错:
_util2.default.axios.all is not a function
代码:
init () {
util.axios.all([this.getCourseInit(), this.getConfirmInit()])
.then(util.axios.spread((indexRes, confirmRes) => {
// 两个请求现在都执行完成
this.classData = indexRes.data.today_course.map(item => {
item.time = timeUtil.formatDate2Str(item.start_time, 'HH:mm') + '~' + timeUtil.formatDate2Str(item.end_time, 'HH:mm');
return item;
});
this.count.count_course_today = indexRes.data.count.count_course_today;
this.count.count_student_not = indexRes.data.count.count_student_not;
this.count.count_student_all = indexRes.data.count.count_student_all;
this.count.count_teacher_all = indexRes.data.count.count_teacher_all;
this.isLoading = false;
}));
},
getCourseInit () {
return util.axios.get('/index');
},
getConfirmInit () {
return util.axios.get('/course-confirm');
},
原因:
axios实例没有all这个方法,all是axios的静态方法
解决办法:
以下方法不是最好的,还没找到更好的解决办法,目前先这样解决。
// 引入axios
import axios from 'axios';
init () {
axios.all([this.getCourseInit(), this.getConfirmInit()])
.then(axios.spread((indexRes, confirmRes) => {
// 两个请求现在都执行完成
this.classData = indexRes.data.today_course.map(item => {
item.time = timeUtil.formatDate2Str(item.start_time, 'HH:mm') + '~' + timeUtil.formatDate2Str(item.end_time, 'HH:mm');
return item;
});
this.count.count_course_today = indexRes.data.count.count_course_today;
this.count.count_student_not = indexRes.data.count.count_student_not;
this.count.count_student_all = indexRes.data.count.count_student_all;
this.count.count_teacher_all = indexRes.data.count.count_teacher_all;
this.isLoading = false;
}));
},
getCourseInit () {
return util.axios.get('/index');
},
getConfirmInit () {
return util.axios.get('/course-confirm');
},
以上这篇解决vue 使用axios.all()方法发起多个请求控制台报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Electron+vue3项目使用SQLite3数据库详细步骤(超详细)
Electron是一个基于vue.js的新框架,它可以构建桌面应用,这篇文章主要给大家介绍了关于Electron+vue3项目使用SQLite3数据库的详细步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下2024-01-01
vue2.0 使用element-ui里的upload组件实现图片预览效果方法
今天小编就为大家分享一篇vue2.0 使用element-ui里的upload组件实现图片预览效果方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-09-09
Vue对Element中的el-tag添加@click事件无效的解决
本文主要介绍了Vue对Element中的el-tag添加@click事件无效的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-05-05


最新评论