vuex持久化vuex-persistedstate,存储的数据刷新页面后导致数据丢失

1.安装插件

vuex-persistedstate实现数据持久化,来解决使用vuex存储状态时页面刷新后数据消失的问题

vuex-persistedstate:在页面重新加载之间保持并重新补充Vuex状态

npm install --save vuex-persistedstate

2.具体的代码, 现在需要持久化存储的 requestUrl

user.js

state: {

requestUrl: ''

},

mutations: {

SET_WEBSITE_ROUTETURL: (state, title) => {

state.requestUrl = title;

},

},

actions: {

SetWebSiteTitle_routeUrl({ commit, state }, title) {

console.log(title)

commit('SET_WEBSITE_ROUTETURL', title);

},

}

index.js

import { createStore } from "vuex";

import app from "./modules/app";

import user from "./modules/user";

import tagsView from "./modules/tagsView";

import permission from "./modules/permission";

// import settings from './modules/settings'

import getters from "./getters";

import dict from './modules/dict'

import createPersistedState from 'vuex-persistedstate';

const vueStore = createStore({

state: {},

mutations: {},

actions: {},

modules: {

app,

user,

tagsView,

permission,

dict,

// settings

},

plugins: [

createPersistedState({

storage: window.localStorage,

reducer(state) {

return {

user: {

requestUrl: state.user.requestUrl, // 只持久化 requestUrl

},

};

},

}),

],

getters,

});

export default vueStore;

3.验证 localStorage 是否存储成功

  1. 打开 Application > Local Storage

  2. 确认是否存在键名 vuex,其值应包含 requestUrl

  3. 如果没有 vuex 键,说明插件未生效。

相关推荐
老毛肚1 天前
jeecgboot vue TS & 模板化 04
前端·javascript·vue.js
晓13131 天前
【Cocos Creator 2.x】篇——第二章 入门
javascript·游戏引擎
AI_零食1 天前
鸿蒙PC Electron跨平台应用开发:24时区时间表应用详解
前端·华为·electron·开源·harmonyos·鸿蒙
z落落1 天前
C# 四种特殊类:抽象类、密封类、静态类、部分类
开发语言·c#
Electrolux1 天前
[onlyoffice-v9]纯前端怎么实现编辑预览office
前端·javascript·github
VidDown1 天前
Webhook 调试器:让第三方回调“原形毕露”
java·开发语言·javascript·编辑器·postman
码云之上1 天前
聊聊如何设计一个高效、稳定的 Node.js 接入层
前端·后端·node.js
装不满的克莱因瓶1 天前
基于 OpenResty 扩展开发实现动态服务注册与发现能力
java·开发语言·架构·openresty
kyriewen1 天前
我读了一遍 Babel 编译后的 async/await,终于搞懂了它的原理(附 20 行手写实现)
前端·javascript·面试
weixin_523185321 天前
Java基础知识总结(四):引用数据类型与参数传递机制
java·开发语言·python