为什么Vuex-persist不将任何内容存储在localStorage中?

时间:2019-06-20 12:00:47

标签: vue.js local-storage vuex vue-cli persist

因此,在我的项目(Vue-cli + TypeScript)中,我需要将用户数据存储到locaStorage。为此,我决定将vuex-persist(npm插件)与vuex一起使用。但是在DevTool中,localStorage中什么都没有出现。我的代码有什么问题。先感谢您。

在先前的项目中,我已经使用了这些工具的组合,并且它们工作正常。在此项目中,我使用相同的配置,但不起作用。这是最奇怪的事情。

这是StructureModule.ts

import { ActionTree, MutationTree, GetterTree, Module } from "vuex";

const namespaced: boolean = true;

interface IDataStructure {
    name: string;
    type: string;
    description: string;
}

interface IStructureState {
    name: string;
    description: string;
    props: IDataStructure[];
}


export interface IState {
    structures: IStructureState[];
}

export const state: IState = {
    structures: [
        {
            name: "",
            description: "",
            props: [
                {
                    name: "",
                    type: "",
                    description: "",
                },
            ],
        },
    ],
};

export const actions: ActionTree<IState, any> = {
    addNewDataStructure({ commit }, payload: IStructureState): void {
        commit("ADD_DATA_STRUCTURE", payload);
    },
    updateDataStructure({ commit }, payload: IStructureState): void {
        commit("UPDATE_EXISTING_DATA_STRUCTURE", payload);
    },
    clearDataStructure({ commit }, { name }: IStructureState): void {
        commit(" CLEAR_DATA_STRUCTURE", name);
    },
};

export const mutations: MutationTree<IState> = {
    ADD_DATA_STRUCTURE(state: IState, payload: IStructureState) {
        if (state.structures[0].name === "") {
            state.structures.splice(0, 1);
        }
        state.structures.push(payload);
    },

    CLEAR_DATA_STRUCTURE(state: IState, name: string) {
        state.structures.filter((structure: IStructureState) => {
            if (structure.name === name) {
                state.structures.splice( state.structures.indexOf(structure), 1);
            }
        });
    },

    UPDATE_EXISTING_DATA_STRUCTURE(state: IState, payload: IStructureState) {
        state.structures.map((structure: IStructureState) => {
            if (structure.name === payload.name) {
                state.structures[state.structures.indexOf(structure)] = payload;
            }
        });
    },
};

export const getters: GetterTree<IState, any> = {
    dataStructureByName(state: IState, structName: string): IStructureState[] {
        const structure: IStructureState[] = state.structures.filter((struct: IStructureState) => {
            if (struct.name === structName) {
                return struct;
            }
        });
        return structure;
    },

    dataStructures(): IStructureState[] {
        return state.structures;
    },
};

export const StructureModule: Module<IState, any> = {
    namespaced,
    state,
    mutations,
    actions,
    getters,
};

这是index.ts

import Vue from "vue";
import Vuex, { ModuleTree } from "vuex";
import VuexPersistence from "vuex-persist";

import { StructureModule , IState} from "./modules/StructureModule";

Vue.use(Vuex);

const storeModules: ModuleTree<IState> = {
    StructureModule,
};

const vuexPersistentSessionStorage = new VuexPersistence({
    key: "test",
    modules: ["StructureModule"],
});


export default new Vuex.Store<any>({
    modules: storeModules,
    plugins: [vuexPersistentSessionStorage.plugin],
});

这是main.ts


import store from "@/store/index.ts";
import * as $ from "jquery";
import Vue from "vue";

import App from "./App.vue";
import router from "./router";

global.EventBus = new Vue();
(global as any).$ = $;
Vue.config.productionTip = false;
console.log(store);
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");

这是vue.config.js

module.exports = {
    transpileDependencies: ["vuex-persist"],
};

This is store in vue-devtool And this is dev-tool localStorage

我希望在localstorage中出现带有键“ test”且具有预定义值的存储,但不是这个localStorage为空。

2 个答案:

答案 0 :(得分:1)

如指南中所述

  

在Vuex存储中实际更改状态的唯一方法是提交   突变

https://vuex.vuejs.org/guide/mutations.html

我在您的代码中看不到任何变异。

否则,您应该看看https://github.com/robinvdvleuten/vuex-persistedstate,它似乎更受欢迎,而且我一直在使用它,没有任何问题。

用法非常简单:您只需要在商店内声明一个插件:

import createPersistedState from 'vuex-persistedstate'

const store = new Vuex.Store({
  // ...
  plugins: [createPersistedState()],
})

答案 1 :(得分:0)

我找到了解决此问题的方法。 就我而言,我只是从中删除命名空间

export const StructureModule: Module<IState, any> = { namespaced, <----- This state, mutations, actions, getters, };

似乎只有在有多个模块的情况下才应使用命名空间。