解决Vuex刷新页面数据丢失的问题

一:数据丢失的原因

  1. 将vuex中的数据直接保存到浏览器缓存中(sessionStorage、localStorage、cookie)
  2. 页面刷新后再从浏览器中取出

二:解决的思路

  1. 将vuex中的数据直接保存到浏览器缓存中(sessionStorage、localStorage、cookie)
  2. 页面刷新后再从浏览器中取出

三:解决方法

方法一:
  1. 直接在vuex修改数据方法中将数据存储到浏览器本地存储中
    import Vue from 'vue';
    import Vuex from 'vuex';
     
    Vue.use(Vuex);
     
    export default new Vuex.Store({
        state: {
           orderList: [],
           menuList: []
       },
        mutations: {
            orderList(s, d) {
              s.orderList= d;
              window.localStorage.setItem("list",jsON.stringify(s.orderList))
            },  
            menuList(s, d) {
              s.menuList = d;
              window.localStorage.setItem("list",jsON.stringify(s.menuList))
           },
       }
    })
    
  2. 在页面加载时再从本地存储中取出并赋给vuex
    if (window.localStorage.getItem("list") ) {
            this.$store.replaceState(Object.assign({}, 
            this.$store.state,JSON.parse(window.localStorage.getItem("list"))))
    }
    

    在此可以进行优化

  3. 通过监听beforeunload事件来进行数据的localStorage存储,beforeunload事件在页面刷新时进行触发,具体做法是在App.vue的created()周期函数中下如下代码:
    if (window.localStorage.getItem("list") ) {
        this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(window.localStorage.getItem("list"))))
    } 
     
    window.addEventListener("beforeunload",()=>{
        window.localStorage.setItem("list",JSON.stringify(this.$store.state))
    })
    
    方法二:
  4. 利用第三方库进行持久化存储安装
  5. vuex-persistedstate
  6. npm install --save vuex-persistedstate

    在store文件夹下的indedx.js中配置信息
    使用vuex-persistedstate默认存储到localStorage
     

    import createPersistedState from "vuex-persistedstate"
    const store =newVuex.Store({
      state: {
        count: 1
      },
      mutations: {},
      actions: {},
      // 当state中的值发生改变,此时localStorage中的vuex的值会同步把state中的所有值存储起来,当页面刷
       新的时候,state的值会从localStorage自动获取vuex的value值,赋值到state中
      plugins: [createPersistedState()] 
    })

  7. 使用vuex-persistedstate存储到sessionStorage
    import createPersistedState from "vuex-persistedstate"
    const store = new Vuex.Store({
       state: {},
       mutations: {},
       actions: {},
       plugins: [createPersistedState({
           storage:window.sessionStorage  // 同localStorage相同,只是将vuex的所有值存储到sessionStorage中
       })]
    })
    

  8. 使用vuex-persistedstate指定需要持久化的state
    import createPersistedState from "vuex-persistedstate"
    
    const store = newVuex.Store({
     state: {
      count: 0
    },
     mutations: {},
     actions: {},
     plugins: [createPersistedState({
       storage:window.sessionStorage,
       reducer(val)  {
             // 此时,当count发生改变的时候,就会调用此函数,并且val的值为当前state对象,return的值为当前本地存储的value值(本地存储的key值为vuex)
             return {
                 count: val.count,
             changeCount: 'aaa'
             }
         }
     })]
    })
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值