gpt4 book ai didi

vue.js - 为什么 Vuex-persist 不在 localStorage 中存储任何东西?

转载 作者:行者123 更新时间:2023-12-01 15:03:36 25 4
gpt4 key购买 nike

因此,在我的项目(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 是空的。

最佳答案

正如指南中所说

The only way to actually change state in a Vuex store is by committinga mutation


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()],
})

关于vue.js - 为什么 Vuex-persist 不在 localStorage 中存储任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56685536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com