当前位置: 首页 > news >正文

vuex数据持久化

清空原因:

刷新页面vuex的数据会丢失属于正常现象,因为JS的数据都是保存在浏览器的堆栈内存里面的,刷新浏览器页面,以前堆栈申请的内存被释放,这就是浏览器的运行机制,那么堆栈里的数据自然就清空了。

解决办法:

1.手动存储
state: {role: localStorage.getItem('role') || '',token: localStorage.getItem('token') || '',
},
actions: {login ({ commit }, { token, role }) {localStorage.setItem('token', token)localStorage.setItem('role', role)commit('setToken', token)commit('setRole', role)}
}

使用localStorage或sessionStorage将vuex存储的数据直接存储在本地。

2.插件存储

本质上是自动存储在localStorage或sessionStorage中。

a.vuex-persistedstate
npm install --save vuex-persistedstate
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'Vue.use(Vuex)export default new Vuex.Store({plugins: [createPersistedState()],state: {},getters: {},mutations: {},actions: {},modules: {}
})
b.vuex-along
npm install vuex-along --save
import VueXAlong from 'vuex-along'Vue.use(Vuex)
const store=new Vuex.Store({modules:{},plugins: [VueXAlong({name: 'along',     //存放在localStroage或者sessionStroage 中的名字local: false,      //是否存放在local中  false 不存放 如果存放按照下面session的配置配session: { list: [], isFilter: true }   //如果值不为false 那么可以传递对象 其中 当isFilter设置为true时, list 数组中的值就会被过滤调,这些值不会存放在seesion或者local中})]})
c.vuex-persist
npm install --save vuex-persist
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)const vuexLocal = new VuexPersistence({storage: window.localStorage
})const store = new Vuex.Store({modules:{},plugins: [vuexLocal.plugin] }) 
http://www.lryc.cn/news/383041.html

相关文章:

  • MySQL之复制(十)
  • Spring MVC数据绑定和响应——简单数据绑定(一)默认类型数据绑定
  • 短视频平台自动化插件编写需要用到的源代码分享!
  • 安卓下载以来总是要添加maven下载地址,放在哪?
  • springboot多数据源应用,A服务依赖于B服务jar包,A服务和B服务业务数据分别入自己的库如何做?
  • 20240626 每日AI必读资讯
  • C语言经典算法题第一题
  • 计算预卷积特征
  • Python 入门 —— 描述器
  • 测试驱动开发TDD
  • [论文笔记]Mixture-of-Agents Enhances Large Language Model Capabilities
  • Redis 7.x 系列【6】数据类型之字符串(String)
  • 指针(一)
  • harmony鸿蒙下实现bc交互的方式和方法
  • 【MySQL进阶之路 | 高级篇】索引的声明与使用
  • 探索Java中的设计模式:从单例到工厂模式
  • 表单(forms)
  • Geoserver源码解读四 REST服务
  • 硬件开发笔记(二十一):外部搜索不到的元器件封装可尝试使用AD21软件的“ManufacturerPart Search”功能
  • 【AI大模型】GPTS 与 Assistants API
  • 攻击者开始使用 XLL 文件进行攻击
  • Why RAG is slower than LLM?
  • Word页码设置,封面无页码,目录摘要阿拉伯数字I,II,III页码,正文开始123为页码
  • 汽车汽配图纸管理、产品研发管理解决方案
  • 小程序简单版音乐播放器
  • 驾校预约管理系统
  • C++ 左值右值 || std::move() || 浅拷贝,深拷贝 || 数据类型
  • 发那科机器人IO 分配
  • ubuntu开机怎么进入、退出命令行界面
  • 『FPGA通信接口』LVDS接口(4)LVDS接收端设计