解決vue 使用axios.all()方法發(fā)起多個(gè)請(qǐng)求控制臺(tái)報(bào)錯(cuò)的問題
今天在項(xiàng)目中使用axios時(shí)發(fā)現(xiàn)axios.all() 方法可以執(zhí)行但是控制臺(tái)報(bào)錯(cuò),后來在論壇中看到是由于axios.all() 方法并沒有掛載到 axios對(duì)象上,需要我們手動(dòng)去添加
== 只需要在你封裝的axios文件里加入 ==
instance.all = axios.all
就完美解決了!
補(bǔ)充知識(shí):vue項(xiàng)目中使用axios.all處理并發(fā)請(qǐng)求報(bào)_util2.default.axios.all is not a function異常
報(bào)錯(cuò):
_util2.default.axios.all is not a function
代碼:
init () { util.axios.all([this.getCourseInit(), this.getConfirmInit()]).then(util.axios.spread((indexRes, confirmRes) => { // 兩個(gè)請(qǐng)求現(xiàn)在都執(zhí)行完成 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實(shí)例沒有all這個(gè)方法,all是axios的靜態(tài)方法
解決辦法:
以下方法不是最好的,還沒找到更好的解決辦法,目前先這樣解決。
// 引入axios import axios from ’axios’; init () { axios.all([this.getCourseInit(), this.getConfirmInit()]).then(axios.spread((indexRes, confirmRes) => { // 兩個(gè)請(qǐng)求現(xiàn)在都執(zhí)行完成 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()方法發(fā)起多個(gè)請(qǐng)求控制臺(tái)報(bào)錯(cuò)的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTML DOM setInterval和clearInterval方法案例詳解2. XML 非法字符(轉(zhuǎn)義字符)3. 不要在HTML中濫用div4. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)5. CSS清除浮動(dòng)方法匯總6. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))7. XML入門的常見問題(三)8. .NET Core 分布式任務(wù)調(diào)度ScheduleMaster詳解9. jscript與vbscript 操作XML元素屬性的代碼10. XML在語音合成中的應(yīng)用
