vue, axios loads data asynchronously, but some files need to use the data obtained asynchronously. Before the data is obtained, the file has already been executed. At this time, the data is empty and an error will be reported. How to solve this problem?
specific performance:
my vue project will call a login method when entering the page, and then set localStorage,
const instance = axios.create({
baseURL: config.BASE_URL,
headers: {
Authorization: localStorage.getItem("Authorization") || ""
}
});
export default {
getList() {
return instance.request({
url: "123",
method: "get"
})
}
}
but because the login method has not finished executing the api.js file, localStorage.getItem ("Authorization")
is empty and causes an error. How to solve this problem?