L
L
Little Vasya2019-03-03 13:17:28
Vue.js
Little Vasya, 2019-03-03 13:17:28

LocalStorage is not defined?

I use the nuxt js framework, when using the localstorage object, it gives out undefined ... I read why this happens, they say that you need to set the proccess.client flag so that .. it works on the client but without the original

getItem (KEY, defaultValue = null) {
    const value = JSON.parse(localStorage.getItem(KEY))
    if (!value) {
      return defaultValue
    }
    return value
  },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
karambafe, 2019-03-03
@emilov

process.client is just a check for the rendering type, not a guarantee of the client one :)
It's not clear in your code where exactly the getItem method is called.
Based on this, I can only say one thing - pull it in the mounted hook, in which the window object is definitely available (localStorage is its property)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question