Vuex数据需要持久化的原因:
因为Vuex的数据保存在内存中,所以当刷新页面的时候,数据会被销毁。
解决方案
方法一:
手动存储在本地存储:localStorage中
方法二:
使用插件:vuex-persistedstate
使用方法:
npm install vuex-persistedstate
第二步:
在vuex的入口文件进行配置
import createPersistedstate from 'vuex-persistedstate'
第二步:
const store = new Vuex.Store({
state:{
shopList:[],
count:10
},
mutations:{
}
},
actions:{
},
getters:{
},
modules:{
counter
},
plugins:[
createPersistedState({
key:'Data_persistence',
paths:['counter']
})
]
})