2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
我的源碼 我創(chuàng)建一個(gè) axios 實(shí)例instance
// respone攔截器
instance.interceptors.response.use(
response => {
return response
},
error => { // 默認(rèn)除了2XX之外的都是錯(cuò)誤的,就會走這里
if (error.response) {
switch (error.response.status) {
case 401:
if (store.state.gameId) {
store.commit('DELETE_TOKEN')
router.replace({ // 跳轉(zhuǎn)到登錄頁面
path: '/login?gameId=' + store.state.gameId,
query: {redirect: router.currentRoute.fullPath, gameId: store.state.gameId} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由
})
} else {
store.commit('USER_LOGOUT')
}
}
}
return Promise.reject(error.response)
}
)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
添加回答
舉報(bào)