How to store data to local storage with Nuxt.js

前端 未结 3 2021
忘了有多久
忘了有多久 2021-02-15 15:02

As you know, nuxtjs is server side rendering and there is no good example how to store data into localstorage which is client side.

My work is need to build login form w

3条回答
  •  故里飘歌
    2021-02-15 15:23

    You can use process.browser to check it's in browser or not.

    export default {
      created() {
        this.storeToken();
      },
      methods:{
        storeToken(token){
          if(process.browser){
              localStorage.setItem("authToken", token);
          }
        }
      }
    }

    You also call this method in mounted without check process.browser.

    export default {
      mounted() {
        this.storeToken();
      },
      methods:{
        storeToken(token){
           localStorage.setItem("authToken", token);
        }
      }
    }

提交回复
热议问题