How to solve data loss after vuex refresh

Lead:

When we write vue projects, we often use vuex as memory for storage, but the data we store in vuex (user data, current page data, etc.) will be lost after refresh, so how to solve it? Super simple.

1. Install vuex-persistedstate

npm install --save vuex-persistedstate

2. Modify the store

import createPersistedState from 'vuex-persistedstate'
 
const store = new Vuex.Store({
    
    
  state: {
    
    
    user: {
    
    }, //用户
  },
  mutations: {
    
    
    update_user(state, newuser) {
    
    
      state.user = newuser
    },
  },
    //把所有数据缓存到本地
  plugins: [createPersistedState()],
})
 
export default store;

After saving, run the program again, the data in vuex will not be lost after refreshing.

Guess you like

Origin blog.csdn.net/gitchatxiaomi/article/details/108914088