Vuex 页面刷新后store保存的数据会丢失 取cookie值
在store.js中
export default new vuex.Store({
// 首先声明一个状态 state
state:{
pcid: '',
postList: [],
}
//更新状态
mutations:{
changepcId(state, _pcid){ state.pcid = _pcid; }, changepostList(state, _postList){ state.postList = _postList; Cookies.set('postList', _postList); },
}
}
在A页面存数据不变
this.$store.commit( 'changepostList', res );
在B页面取数据
data(){ return{ postimg: '', datalist:[], } },
created(){//页面刷新后store保存的数据会丢失 取cookie值 if(this.$store.state.postList.length ===0 ){ this.datalist =JSON.parse(Cookies.get('postList')) ; }else{ this.datalist = this.$store.state.postList; } if(this.postimg === ''){ this.postimg = Cookies.get('postimg'); }else{ this.postimg = this.$store.state.postimg; } },