N
N
Nazar2019-03-22 11:28:36
Vue.js
Nazar, 2019-03-22 11:28:36

How to read cookie/localStorage before creating Vue component?

Just starting to learn about cookies and how to work with them. When creating a component, I want to check whether there is a necessary cookie and if the result is successful / not successful, display different information. But I can only do this at the beforeMount() stage. When using check in created() it writes localStorage or document is not defined. How can and can this problem be solved?
In the project I used vue-cookie and localStorage.setItem(key) on a nuxt project.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-03-22
@0xD34F Vue.js

created() writes localStorage or document is not defined <...> nuxt

I've heard that when using server rendering, the beforeCreate and created hooks are executed on both the server and the client. Naturally, neither localStorage nor document will be on the server. I also heard that you can check where the code is executing:
if (process.client) {
  // здесь можете пощупать localStorage
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question