[Vuejs]-How to get access localStorage inside asyncData in Nuxt js

2👍

localStorage is not defined because asyncData() is resolved on the server during SSR. The localStorage variable is only accessible in the client browser.

See documentation: https://nuxtjs.org/docs/features/data-fetching/. You can use localStorage in the mounted() hook.

👤Eflyax

2👍

In addition to Jakub Záruba’s answer, if you need to access localStorage in asyncData nonetheless, you can modify your if-statement like this:

if (process.client && localStorage.getItem("myCat")) {
  alert(localStorage.getItem("myCat"))
  
  return
}

so you will run this piece of code only on the client-side.

Leave a comment