一段时间以来,我遇到了一个相当大的问题,并且让我很紧张,这没有意义。我在我的 React 前端使用了 axios,它在将 get 值分配给状态时效果很好。但是当在普通的 javascript 代码中使用它时,我似乎有以下问题:我可以在控制台中打印对象的值,但它只会返回 undefined .. 这是我的代码:
login = () => {
let data;
axios.get('https://myaddress/authenticate')
.then(response => {
data = response;
console.log('data here', data);
})
.catch(error => {
console.error('auth.error', error);
});
console.log('eee', data);
return data;
};Run Code Online (Sandbox Code Playgroud)
这里我们严格地谈论 axios。