当前位置:网站首页>vuex中利用缓存解决刷新state数据丢失问题
vuex中利用缓存解决刷新state数据丢失问题
2022-06-26 18:10:00 【Whoopsina】
在使用vuex的过程中,经常会遇到刷新页面时state中数据丢失,此时可以利用本地缓存解决
注意:
实例代码为uniapp项目,可以将缓存方法转换为原生
//在index.js中
state: {
userInfo: null
},
getters:{
userInfo(state) {
if(!state.userInfo){
state.userInfo = uni.getStorageSync('userInfo');
}
return state.userInfo
}
},
mutations: {
setUserInfo(state,data) {
state.userInfo = data;
uni.setStorageSync('userInfo',data);
}
},
// 在组件中state和getters中都可以拿到
computed:{
...mapGetters(['userInfo']);
...mapState(['userInfo']);
},
注意
自己在使用vuex中遇到的坑:
vuex官方文档: https://vuex.vuejs.org/zh/
- data中的属性与computed中的属性不可以重名,
但是state和getters中的属性可以重名,互不干涉。 - 页面要使用的vuex数据较多时,可以多多使用…mapState,…mapGetters等等
- 待更新
边栏推荐
- Do you know how to compare two objects
- Numpy之matplotlib
- transforms.RandomCrop()的输入只能是PIL image 不能是tensor
- Image binarization
- wm_ Concat() and group_ Concat() function
- 博云,站在中国容器潮头
- Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
- ROS查询话题具体内容常用指令
- Detailed explanation of dos and attack methods
- Comparing the size relationship between two objects turns out to be so fancy
猜你喜欢

tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板

成功解决之idea引用Lombok的@Slf4j后无法正常使用log

Bayesian network explanation

Discussion and generation of digital signature and analysis of its advantages

行锁分析和死锁

Leetcode interview question 29 clockwise print matrix

(multi threading knowledge points that must be mastered) understand threads, create threads, common methods and properties of using threads, and the meaning of thread state and state transition

深层次安全定义剖析及加密技术

VCD video disc

深入理解MySQL锁与事务隔离级别
随机推荐
wm_ Concat() and group_ Concat() function
Introduction to Ethereum Technology Architecture
pycharm的plt.show()如何保持不关闭
Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
ZCMU--1367: Data Structure
tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
VCD-影音光碟
The cross compilation environment appears So link file not found problem
Row lock analysis and deadlock
50行代码爬取Top500图书导入TXT文档
Handwritten promise all
基于tensorflow的手写数字识别
PC端录制扫515地机器人/scan数据
[unity] use C in unity to execute external files, such as Exe or bat
数据加密标准(DES)概念及工作原理
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
In and exceptions, count (*) query optimization
Binary search-1
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
JVM入個門(1)