这篇文章主要介绍了关于vuex强刷数据丢失问题解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了关于vuex强刷数据丢失问题解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了关于vuex强刷数据丢失问题解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
$ npm install vuex-persistedstate -S|
1
2
3
4
5
|
import persistedState from 'vuex-persistedstate'const store = new Vuex.Store({ // ... plugins: [persistedState()]}) |
vuex-persistedstate默认使用localStorage储存,若想使用sessionStorage,可采用以下配置
|
1
2
3
4
5
6
7
|
import persistedState from "vuex-persistedstate"const store = new Vuex.Store({ // ... plugins: [persistedState ({ storage: window.sessionStorage })]}) |
$ npm install js-cookie -S|
1
2
3
4
5
6
7
8
9
10
11
12
|
import Cookies from 'js-cookie';import persistedState from "vuex-persistedstate"const store = new Vuex.Store({ // ... plugins: [persistedState ({ storage: { getItem: key => Cookies.get(key), setItem: (key, value) => Cookies.set(key, value), removeItem: key => Cookies.remove(key) } })]}) |
vuex-persistedstate插件,vuex-persistedstate是没有加密的,用户的信息就暴露在缓存中,secure-ls来加密storage$ npm install secure-ls -S|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import Vue from "vue";import Vuex from "vuex";import SecureLS from 'secure-ls';import persistedState from "vuex-persistedstate";const ls = new SecureLS({ encodingType: "aes", // 加密方式 isCompression: false, // 是否启用数据压缩 encryptionSecret: "old-beauty" // });Vue.use(Vuex);export default new Vuex.Store({ ... plugins: [persistedState({ // key: "123123", // 存入storage是的key storage: { getItem: key => ls.get(key), setItem: (key, value) => ls.set(key, value), removeItem: key => ls.remove(key) } })],}); |
【注】vuex-persist(不兼容ie) vuex-persistedstate
到此这篇关于vuex强刷数据丢失的文章就介绍到这了,更多相关vuex数据丢失内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://blog.csdn.net/qq_43014319/article/details/115353329
发表评论