store from localStorage in nuxt is restored after mounted()
See original GitHub issueI’m not sure if this is the expected behavior but it certainly makes things more complicated. I thought the state would be restored if localStorage
is used. But it turns out that’s not the case.
Here’s how I set it up.
// ~/plugins/vuex-persist.js
import VuexPersistence from 'vuex-persist'
export default ({ store }) => {
window.onNuxtReady(() => {
new VuexPersistence({
storage: window.localStorage,
reducer: (state) => ({
settings: {
...state.settings,
},
}).plugin(store);
});
}
And when I log the state.settings
object in a mounted()
hook, I get the store defaults instead of the values from localStorage
. Also the store.restored
value seems to be always undefined
. So I came up with my own work around:
// ~/store/settings.js
const state = {
// ...
restored: false,
}
// ~/nuxt.config.js
{
// ...
plugins: [
{ src: '~/plugins/vuex-persist', ssr: false },
],
}
// ~/plugins/vuex-persist.js
import VuexPersistence from 'vuex-persist'
export default ({ store }) => {
window.onNuxtReady(() => {
new VuexPersistence({
storage: window.localStorage,
reducer: (state) => ({
settings: {
...state.settings,
restored: true,
},
}).plugin(store);
});
}
// pages/test.vue
export default {
watch:{
restored(val){
if(val === true){
// do things
}
}
},
computed:{
restored(){
return this.$store.state.settings.restored;
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top Results From Across the Web
How do I access localStorage in store of NuxtJs?
When you use SSR you don't have access to the browser storage. To do a workaround, you can dispatch an action on the...
Read more >[Solved]-Middleware executing before Vuex Store restore from ...
Now nuxt has a function called nuxtServerInit() in vuex store index file. You should use it to retrieve the token from request and...
Read more >Create a shopping cart with Nuxt 3 | EnterFlash Blog
During this tutorial, we'll be using Nuxt 3 to create a persistent shopping cart that saves its information in local storage to persist...
Read more >vuex-persist - Bountysource
Create nuxt project and add vuex-presist. store/index.js. import Vue from 'vue'; export const state = () => ({ preview: {}, templateR: false })...
Read more >nuxt-vuex-localstorage - npm Package Health Analysis - Snyk
Since local storage updates as store changes, server side function such as fetch, asyncData can be used before components are mounted. <script> export...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@snakemastr when you say “global” what do you mean?
fwiw if you meant the store persists between applications in the same session, then use the
key
parameter. we had the same issue.edit: my instructions edited to include this fact.
we had to implement it a different way from the instructions in the readme to get it work for us.
no promises! ymmv! etc
instead of using
vuex-persist
as a nuxt plugin and adding the suggested file into the./plugins
directory and the config, we just imported it directly in ourstore
file, like so:We also still do the transpile step in the nuxt config, i.e.
It’s possible that the plugin author’s instructions are simply dated. Things change a lot in nuxt minor versions!