gpt4 book ai didi

javascript - Axios/Vue - 防止 axios.all() 继续执行

转载 作者:可可西里 更新时间:2023-11-01 01:19:08 33 4
gpt4 key购买 nike

在我的应用程序中,为了对用户进行身份验证,我调用了 fetchData 函数。如果用户 token 无效,应用程序将运行 axios.all(),我的拦截器将返回大量错误。

如何防止 axios.all() 在第一个错误后继续运行?并且只向用户显示一条通知?

拦截器.js

export default (http, store, router) => {
http.interceptors.response.use(response => response, (error) => {
const {response} = error;

let message = 'Ops. Algo de errado aconteceu...';

if([401].indexOf(response.status) > -1){
localforage.removeItem('token');

router.push({
name: 'login'
});

Vue.notify({
group: 'panel',
type: 'error',
duration: 5000,
text: response.data.message ? response.data.message : message
});
}

return Promise.reject(error);
})
}

auth.js

const actions = {
fetchData({commit, dispatch}) {
function getChannels() {
return http.get('channels')
}

function getContacts() {
return http.get('conversations')
}

function getEventActions() {
return http.get('events/actions')
}

// 20 more functions calls

axios.all([
getChannels(),
getContacts(),
getEventActions()
]).then(axios.spread(function (channels, contacts, eventActions) {
dispatch('channels/setChannels', channels.data, {root: true})
dispatch('contacts/setContacts', contacts.data, {root: true})
dispatch('events/setActions', eventActions.data, {root: true})
}))
}
}

最佳答案

编辑:@tony19's answer更好,因为它允许取消在第一次错误后仍未决的请求,并且不需要任何额外的库。


一个解决方案是为您同时使用的所有请求分配一个唯一标识符(我将在本例中使用 uuid/v4 包,您可以随意使用其他东西):

import uuid from 'uuid/v4'

const actions = {
fetchData({commit, dispatch}) {
const config = {
_uuid: uuid()
}

function getChannels() {
return http.get('channels', config)
}

function getContacts() {
return http.get('conversations', config)
}

function getEventActions() {
return http.get('events/actions', config)
}

// 20 more functions calls

axios.all([
getChannels(),
getContacts(),
getEventActions()
]).then(axios.spread(function (channels, contacts, eventActions) {
dispatch('channels/setChannels', channels.data, {root: true})
dispatch('contacts/setContacts', contacts.data, {root: true})
dispatch('events/setActions', eventActions.data, {root: true})
}))
}
}

然后,在您的拦截器中,您可以选择使用此唯一标识符一次性处理错误:

export default (http, store, router) => {
// Here, you create a variable that memorize all the uuid that have
// already been handled
const handledErrors = {}
http.interceptors.response.use(response => response, (error) => {
// Here, you check if you have already handled the error
if (error.config._uuid && handledErrors[error.config._uuid]) {
return Promise.reject(error)
}

// If the request contains a uuid, you tell
// the handledErrors variable that you handled
// this particular uuid
if (error.config._uuid) {
handledErrors[error.config._uuid] = true
}

// And then you continue on your normal behavior

const {response} = error;

let message = 'Ops. Algo de errado aconteceu...';

if([401].indexOf(response.status) > -1){
localforage.removeItem('token');

router.push({
name: 'login'
});

Vue.notify({
group: 'panel',
type: 'error',
duration: 5000,
text: response.data.message ? response.data.message : message
});
}

return Promise.reject(error);
})
}

补充说明,您可以将 fetchData 函数简化为:

const actions = {
fetchData({commit, dispatch}) {
const config = {
_uuid: uuid()
}

const calls = [
'channels',
'conversations',
'events/actions'
].map(call => http.get(call, config))

// 20 more functions calls

axios.all(calls).then(axios.spread(function (channels, contacts, eventActions) {
dispatch('channels/setChannels', channels.data, {root: true})
dispatch('contacts/setContacts', contacts.data, {root: true})
dispatch('events/setActions', eventActions.data, {root: true})
}))
}
}

关于javascript - Axios/Vue - 防止 axios.all() 继续执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54777218/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com