Vuex 页面刷新后store保存的数据会丢失 取cookie值

在store.js中

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;
            }
        },

猜你喜欢

转载自www.cnblogs.com/dudu123/p/10220474.html
今日推荐