Hello everyone! I'm new to Vue.js and have encountered an issue with my api module. The code looks like this:
axios.create({
baseURL: URL,
headers: {
Authorization: 'Bearer ${localStorage.getItem('token')}'
}
})
When on the login page without a token, the module is already loaded as it's used for the login request. After a successful login, I update the token using
localStorage.setItem('token', token)
. However, the api module doesn't reflect this change until I manually refresh the browser page, causing my requests to have a null token until then.
How can this issue be resolved?
Initially, I considered forcing a page reload after login by switching from this.$router.push('/')
to this.$router.go('/')
, but this broke the navigation to the index page.
I would greatly appreciate any suggestions or solutions. Thank you.