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

相关推荐
hh随便起个名4 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
小浣熊熊熊熊熊熊熊丶4 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子4 小时前
JDK 安装配置
java·开发语言
星哥说事4 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
我是小路路呀5 小时前
element级联选择器:已选中一个二级节点,随后又点击了一个一级节点(仅浏览,未确认选择),此时下拉框失去焦点并关闭
javascript·vue.js·elementui
程序员爱钓鱼5 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
PineappleCoder5 小时前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
等....5 小时前
Miniconda使用
开发语言·python
zfj3215 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧5 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea