Answer the question
In order to leave comments, you need to log in
How to access localStorage in store, namely state, using Nuxt.js?
I am trying to access localStorage in store, namely state.
I understand that there is no access to browser methods/objects in SSR.
I'm trying to do this:
export const state = () => {
if (process.client) {
return {
isAuth: localStorage.getItem('isAuth')
}
}
}
export const state = () => {
if (process.client) {
return {
isAuth: localStorage.getItem('isAuth')
}
} else {
return {
isAuth: 'SomeData'
}
}
}
Answer the question
In order to leave comments, you need to log in
I solved this problem in the following way:
In the layout/default where the Nuxt component is located, in the mounted hook I do the dispatch:
setLocalStorage(state) {
state.isAuth = localStorage.getItem('isAuth')
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question