解決vue 使用axios.all()方法發(fā)起多個(gè)請(qǐng)求控制臺(tái)報(bào)錯(cuò)的問(wèn)題
今天在項(xiàng)目中使用axios時(shí)發(fā)現(xiàn)axios.all() 方法可以執(zhí)行但是控制臺(tái)報(bào)錯(cuò),后來(lái)在論壇中看到是由于axios.all() 方法并沒(méi)有掛載到 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í)例沒(méi)有all這個(gè)方法,all是axios的靜態(tài)方法
解決辦法:
以下方法不是最好的,還沒(mé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ò)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue build過(guò)程取消console debugger控制臺(tái)信息輸出方法詳解
- vue控制臺(tái)警告Runtime directive used on component with non-element root node
- 使用vue-i18n?入口文件配置控制臺(tái)報(bào)警問(wèn)題解決
- Vue如何通過(guò)瀏覽器控制臺(tái)查看全局data值
- 安裝vue3開(kāi)發(fā)者工具但控制臺(tái)沒(méi)有顯示出vue選項(xiàng)的解決
- 解決Vue控制臺(tái)報(bào)錯(cuò)Failed to mount component: template or render function not defined.
相關(guān)文章
Electron+vue3項(xiàng)目使用SQLite3數(shù)據(jù)庫(kù)詳細(xì)步驟(超詳細(xì))
Electron是一個(gè)基于vue.js的新框架,它可以構(gòu)建桌面應(yīng)用,這篇文章主要給大家介紹了關(guān)于Electron+vue3項(xiàng)目使用SQLite3數(shù)據(jù)庫(kù)的詳細(xì)步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例
本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法
今天小編就為大家分享一篇vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue對(duì)Element中的el-tag添加@click事件無(wú)效的解決
本文主要介紹了Vue對(duì)Element中的el-tag添加@click事件無(wú)效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

