解決vue 使用axios.all()方法發(fā)起多個請求控制臺報錯的問題
今天在項目中使用axios時發(fā)現(xiàn)axios.all() 方法可以執(zhí)行但是控制臺報錯,后來在論壇中看到是由于axios.all() 方法并沒有掛載到 axios對象上,需要我們手動去添加
== 只需要在你封裝的axios文件里加入 ==
instance.all = axios.all
就完美解決了!
補充知識:vue項目中使用axios.all處理并發(fā)請求報_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) => { // 兩個請求現(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實例沒有all這個方法,all是axios的靜態(tài)方法
解決辦法:
以下方法不是最好的,還沒找到更好的解決辦法,目前先這樣解決。
// 引入axios import axios from 'axios'; init () { axios.all([this.getCourseInit(), this.getConfirmInit()]) .then(axios.spread((indexRes, confirmRes) => { // 兩個請求現(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ā)起多個請求控制臺報錯的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue基于mint-ui的城市選擇3級聯(lián)動的示例
本篇文章主要介紹了vue基于mint-ui的城市選擇3級聯(lián)動的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10深入了解Vue3中偵聽器watcher的實現(xiàn)原理
在平時的開發(fā)工作中,我們經(jīng)常使用偵聽器幫助我們?nèi)ビ^察某個數(shù)據(jù)的變化然后去執(zhí)行一段邏輯。在?Vue.js?2.x?中,你可以通過?watch?選項去初始化一個偵聽器,稱作?watcher。本文將詳細為大家介紹一下偵聽器的實現(xiàn)原理,需要的可以參考一下2022-04-04淺談vue實現(xiàn)數(shù)據(jù)監(jiān)聽的函數(shù) Object.defineProperty
本篇文章主要介紹了淺談vue實現(xiàn)數(shù)據(jù)監(jiān)聽的函數(shù) Object.defineProperty,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06vue中html2canvas給指定區(qū)域添加滿屏水印
本文主要介紹了vue中html2canvas給指定區(qū)域添加滿屏水印,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11