解决vuex数据异步造成初始化的时候没值报错问题
作者:陈小穗子呀 时间:2024-04-30 10:46:02
当使用vue做登录的时候,我们会把拿到的部分用户信息存在vuex+cookie中,我们知道,vuex的数据是会随着浏览器刷新而丢失的,此时我们会重新请求接口对vuex的数据进行再次赋值,如下
当有cookie但是vuex没有数据的时候再次请求赋值
state
mutation
vuex的数据是异步的,当我们页面需要直接拿vuex的值进行渲染的时候,初始化瞬间(即请求过程进行的时候),vuex是没有这个值的,如何解决这个问题,暂时总结了三种方法,希望有遇到的大佬进行补充和查错:
1.通过watch和mouted监听并且初始化vuex数据
在mounted的时候初始化该数据 并且watch该数据(不推荐)
2.在computed赋值该数据 并且在dom渲染的时候做非空的判断
或者:
3.使用拓展运算符
来源:https://blog.csdn.net/ink_if/article/details/86076720
标签:vuex,数据异步,初始化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python构建自定义回调函数详解
2023-09-07 03:30:45
python中pyplot直方图的绘制方式
2023-11-20 07:58:17
![](https://img.aspxhome.com/file/2023/1/63541_0s.png)
数据库Sql实现截取时间段和日期实例(SQL时间截取)
2024-01-27 01:38:29
![](https://img.aspxhome.com/file/2023/9/110319_0s.png)
Selenium元素定位的30种方式(史上最全)
2023-11-13 17:03:41
![](https://img.aspxhome.com/file/2023/7/66877_0s.png)
vue组件之间进行传值的方法
2024-04-27 15:48:04
![](https://img.aspxhome.com/file/2023/9/133149_0s.png)
Python NumPy中的随机数及ufuncs函数使用示例详解
2021-09-22 15:29:08
![](https://img.aspxhome.com/file/2023/8/93588_0s.png)
Python中矩阵库Numpy基本操作详解
2021-07-09 18:13:05
Django的restframework接口框架自定义返回数据格式的示例详解
2023-06-13 05:13:51
![](https://img.aspxhome.com/file/2023/4/60954_0s.png)
python3 sorted 如何实现自定义排序标准
2022-09-19 21:32:49
JavaScript字符串对象substr方法入门实例(用于截取字符串)
2024-06-05 09:53:44
Python利用matplotlib生成图片背景及图例透明的效果
2023-08-22 08:35:18
![](https://img.aspxhome.com/file/2023/1/110271_0s.jpg)
python自动化测试selenium定位frame及iframe示例
2023-01-08 16:25:28
![](https://img.aspxhome.com/file/2023/5/110395_0s.png)
SQLSERVER的非聚集索引结构深度理解
2024-01-20 01:54:00
![](https://img.aspxhome.com/file/2023/1/85601_0s.bmp)
解决Pandas to_json()中文乱码,转化为json数组的问题
2023-12-05 19:06:33
![](https://img.aspxhome.com/file/2023/1/115001_0s.jpg)
Warning: require(): open_basedir restriction in effect,目录配置open_basedir报错问题分析
2023-06-02 23:28:18
![](https://img.aspxhome.com/file/2023/2/55362_0s.png)
MySql使用mysqldump 导入与导出方法总结
2024-01-23 13:26:32
![](https://img.aspxhome.com/file/2023/2/74782_0s.png)
详解Go语言中关于包导入必学的 8 个知识点
2023-07-09 05:38:11
![](https://img.aspxhome.com/file/2023/9/87619_0s.png)
Python实现的HMacMD5加密算法示例
2022-01-15 06:05:31
python 数据提取及拆分的实现代码
2023-11-13 09:13:12
![](https://img.aspxhome.com/file/2023/1/63581_0s.png)
Go语言中的IO操作及Flag包的用法
2024-04-27 15:31:31
![](https://img.aspxhome.com/file/2023/8/134058_0s.png)