[Vuejs]-Cannot initialise store from localStorage on App initialization

0👍

I’ve solved this with a bit of experimentation and comments from other posters around what is called SSR and SPA.

Firstly, this https://github.com/nuxt/nuxt.js/issues/1500 thread really helped me and the final comment from @jsonberry steered my mind in the right direction, away from fetch and asyncData.

I finally had a bit more of an understanding of how NUXT.js was separating SSR and SPA calls.

I then tried @robyedlin suggestion of putting localStorage initialization in the created() method for my main layout/default.vue page.

While I made progress with that suggestion it turns out created() is also called SSR and I was still trying to initialize my store from credentials that weren’t accessible.

Finally, moving the initialization to mounted() did the trick!

So in summary:

  • My account store is left alone, I don’t try and initialize it when it is created (it’s just overwritten at some point when the SSR stuff runs)
  • On mounted() in layout/defualt.vue I read from localStorage and initialize the account store so I can start making API requests with the appropriate accessToken.

That seems to have done the trick.

👤JMac

Leave a comment