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 键,说明插件未生效。

相关推荐
Thera77731 分钟前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
踩着两条虫1 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
炘爚1 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon1 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头2 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141592 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
Moksha2622 小时前
5G、VoNR基本概念
开发语言·5g·php
jzlhll1232 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
W.D.小糊涂2 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
用头发抵命3 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript