vuex的持久化

  1. 安装 vuex-persist
yarn add vuex-persist
  1. 应用 store.js
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'

Vue.use(Vuex)
const vuexLocal = new VuexPersistence({
  key: 'state', // The key to store the state on in the storage provider.
  storage: window.localStorage, // or window.sessionStorage or localForage
})

export default new Vuex.Store({
  state: {
    user: {},
  },
  getters: {
    user: state => state.user
  },
  actions: {
  },
  modules: {
  },
  plugins: [vuexLocal.plugin]
})
  1. main.js 引入store.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
posted @ 2022-07-05 11:05  shine_lovely  阅读(92)  评论(0编辑  收藏  举报