第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Axios配置文件中this為undefined?

Axios配置文件中this為undefined?

寶慕林4294392 2019-05-11 16:21:41
問題描述在this.$router發(fā)現(xiàn)沒這個(gè)屬性,后面查了下this為undefined相關(guān)代碼//請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)importaxiosfrom'axios'import{Loading,Message}from'element-ui'importDebouncefrom'lodash/debounce'constAxios=axios.create({baseURL:'/',timeout:'10000',responseType:'json',withCredentials:true,headers:{"Content-Type":"application/json;charset=UTF-8"}});letneedLoadingRequestCount=0;letloadingObj;Axios.interceptors.request.use(config=>{console.log(this);if(config.showLoading){showFullScreenLoading();}returnconfig;},error=>{if(needLoadingRequestCount!==0){loadingObj.close();}Message({showClose:true,message:'服務(wù)器有誤!請(qǐng)稍后重試',type:"error"});returnPromise.reject(error);});Axios.interceptors.response.use(res=>{//console.log(this);if(res.config.showLoading){tryHideFullScreenLoading();}if(res.data.code!==200&&res.data.message){Message({showClose:false,type:"error",center:true,message:res.data.message?res.data.message:"服務(wù)器繁忙!請(qǐng)重試"});if(res.data.code===9999100){this.$router.push('/login');}returnPromise.reject(res.data.message);}returnres;},error=>{if(needLoadingRequestCount!==0){loadingObj.close();}Message({showClose:true,message:'服務(wù)器繁忙!請(qǐng)稍后重試',type:"error"});returnPromise.reject(error);});functionshowFullScreenLoading(){if(needLoadingRequestCount===0){loadingObj=Loading.service({lock:true,text:"Loading",spinner:'el-icon-loading',background:'rgba(0,0,0,0.7)'});}needLoadingRequestCount++}functiontryHideFullScreenLoading(){if(needLoadingRequestCountAxios.post(url,data,config),get:(url,config={showLoading:true})=>Axios.get(url,config),delete:(url,config={showLoading:true})=>Axios.delete(url,config)}你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?明明都是箭頭函數(shù)啊,this怎么會(huì)丟呢
查看完整描述

2 回答

?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

樓主,你好!當(dāng)前的this已經(jīng)換過了,不再是vue實(shí)例化的對(duì)象了。所以可以再main.js里初始化實(shí)例對(duì)象的時(shí)候掛載到window下面。
//main.js
...
window.vm=newVue({});
...
//axios
window.vm.$router.push('..')
                            
查看完整回答
反對(duì) 回復(fù) 2019-05-11
  • 2 回答
  • 0 關(guān)注
  • 1473 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)