2016-11-15 82 views
0

我想用愛可信庫發送多個請求。因此,根據docs,我可以all方法做到這一點。這是例子:爲什麼我需要在axios.all回調中使用傳播函數?

function getUserAccount() { 
    return axios.get('/user/12345'); 
} 

function getUserPermissions() { 
    return axios.get('/user/12345/permissions'); 
} 

axios.all([getUserAccount(), getUserPermissions()]) 
    .then(axios.spread(function (acct, perms) { 
    // Both requests are now complete 
    })); 

可是爲什麼我需要寫的

.then(axios.spread(function (acct, perms) { 
    // Both requests are now complete 
    })); 

代替

.then(function (acct, perms) { 
     // Both requests are now complete 
     }); 

如果它也能正常工作?

回答

0

您需要使用axios.spread,因爲它使用的參數數組蔓延到多個參數。這可以防止在您使用axios.all進行多個ajax請求時發生錯誤。

axios.all([ 
axios.get('https://api.github.com/users/abc'); 
axios.get('https://api.github.com/users/abc/repos') 
]) 
.then(axios.spread(function (userResponse, reposResponse) { 
    console.log('User', userResponse.data); 
    console.log('Repositories', reposResponse.data); 
})); 
+0

所以,可以用我的AJAX請求發生什麼,如果我不使用'spread'? – JustLogin

+0

我不是在愛可信的專家而言,而是一個數組是不是爭論我懷疑一個有效的輸入。 –

+0

我不是在愛可信的專家而言,而是一個數組是不是一個說法我懷疑一個有效的輸入。 –

相關問題