我试图运行axios.all()
,但我收到了一个奇怪的错误。
使用Promise
时,效果很好:
Promise.all([
axios.get(`/conversation/api/totalCount/${Cookies.get('id')}`),
axios.get('/api/all-members')
]).then(([totalCount, allMembers]) => {
console.log('totalCount\n', totalCount) // I see the result
console.log('allMembers\n', allMembers) // I see the result
})
但是对于axios
,它会在承诺解决之前抛出错误:
axios.all([
axios.get(`/conversation/api/totalCount/${Cookies.get('id')}`),
axios.get('/api/all-members')
]).then(axios.spread((totalCount, allMembers)) => {
// Uncaught SyntaxError: Unexpected token .
console.log('totalCount\n', totalCount)
console.log('allMembers\n', allMembers)
})
Uncaught SyntaxError: Unexpected token .
错误发生在promise解析之前(.then()
)。
我关注this issue和文档here。不确定我做错了什么。顺便说一句,这是所有客户端JS(这可能是它无法正常工作的原因吗?)。
如何正确使用axios.all()
?
答案 0 :(得分:0)
您的括号已关闭。它应该是这样的
axios.all([
axios.get(`/conversation/api/totalCount/${Cookies.get('id')}`),
axios.get('/api/all-members')
]).then(axios.spread((totalCount, allMembers) => { // I have removed a parenthesis here.
// Uncaught SyntaxError: Unexpected token .
console.log('totalCount\n', totalCount)
console.log('allMembers\n', allMembers)
})