首页 > 解决方案 > 当页面刷新状态在nuxt js中为空时

问题描述

我是 Nuxt 的新手,我使用通用模式创建了一个项目,在页面刷新时维护用户状态时遇到问题,商店和身份验证模块在刷新时消失,因此无法在刷新时从商店中检索值。

标签: nuxt.js

解决方案


export const state = () => ({
    data: []
})

//payload hold all form details,
//coming from home.vue
//use spread operator to access all form details

export const mutations = {
  SET(state, data) {
    state.data = data
  }
}

export const actions = {
  async addRootUser({commit}, user){

    await this.$axios.$post( 'http://localhost:3001/owner',user ,{ header: {
        'content-type': 'application/x-www-form-urlencoded'
      } })
      .then(response => {
        console.log('hjfhjfh',response)
        this.$router.push("/family");
        //state.rootUsers = response
        commit('SET', response)
        if(process.browser){
          localStorage.setItem("rootUser", response._id);
          console.log('Set')
        }
        return response
      }).catch((error) => {
        console.log(error.response)
        //this.errors = error.response.data.errors;
        console.log('somthing went wrong.................')
      })
  }
}

[enter image description here][1]


  [1]: https://i.stack.imgur.com/82q8b.png

推荐阅读