vuex 部分模块数据持久化

  • 安装 vuex-persistedstate
npm install --save vuex-persistedstate
  • admin.js
const state = {
  name: '',
  phone: '',
  avatar: ''
}

const mutations = {
}

export default {
  namespaced: true,
  state,
  mutations
}
  • store/index.js
// ...
import createPersistedState from 'vuex-persistedstate'
import admin from './modules/admin'

const adminState = createPersistedState({
  paths: ['admin']
  // paths: ['admin.name']  // admin.js中的'name'持久化
})

// sessionStorage
const activePath = createPersistedState({
  storage: window.sessionStorage,
  paths: ['activeIndex']
})

export default new Vuex.Store({
  // ...
  plugins: [adminState,activePath]
})

脉望

posted @ 2021-06-15 20:53  脉望  阅读(374)  评论(0编辑  收藏  举报