首页 > 社区问答列表 >在Vue中使用localStorage。在哪里需要使用它?

  在Vue中使用localStorage。在哪里需要使用它?

我想使用localStorage,这样当页面重新加载时,数据能够保存下来。

我使用Vuex来追踪一个人是否已注册。并且希望当页面加载时,这个人已经注册。

我的Vuex代码如下:


export default {
    state:{
        isRegistered:false
    },
    actions:{
        registrate({commit}) {
            commit("login")
        },
        exit({commit}) {
            commit("logout")
        }
    },
    mutations:{
        login(state) {
            state.isRegistered=true;
        },
        logout(state) {
            state.isRegistered=false
        }
    },
    getters:{
        IS_REGISTERED(state){
            return state.isRegistered
        }
    }
}

我的Vue.js中的链接




 在重新加载页面时,最好在哪里使用localStorage来保存IS_REGISTERED?


P粉122932466
P粉122932466

  • P粉680000555
  • P粉680000555   采纳为最佳   2023-08-04 16:48:47 1楼

    Translation: 我自己解决了这个问题,// npm-> npm install vuex-persistedstate // OR // yarn-> yarn add vuex-persistedstate

    在我的store中,需要像这样添加:


    import Vue from "vue"
    import Vuex from "vuex"
    import createPersistedState from "vuex-persistedstate";
    
    export default new Vuex.Store({
       modules:{
           // 
       },
       plugins:[createPersistedState()]
    }

    +0 添加回复