vuex做缓存

/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-19 11:44:09
 * @LastEditTime: 2019-10-16 19:35:51
 * @LastEditors: Please set LastEditors
 */
import Vue from 'vue'
import Vuex from 'vuex'
import MainStore from '@_src/pages/main/store/index'
import LoginStore from '@_src/pages/login/store/index'
import createPersistedState from 'vuex-persistedstate'
import { Base64 } from 'js-base64'
const global = {
  namespaced: true,
  state: Gstate,
  mutations: Gmutations,
  actions: Gactions,
  getters: Ggetters
}
// 自定义vuex插件(没有用到)
const myPlugin = store => {
  // 当 store 初始化后调用
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    // mutation 的格式为 { type, payload }
    console.log('state', state)
  })
}
const store = {
  init(Vue, Vuex) {
    Vue.use(Vuex)

    return new Vuex.Store({
      modules: {
        global,
        index: indexStore,
        main: MainStore,
        login: LoginStore
      },
      plugins: [
        myPlugin,
        createPersistedState({
          storage: {
            getItem: key => Base64.decode(window.sessionStorage.getItem(key)),
            setItem: (key, value) =>
              window.sessionStorage.setItem(key, Base64.encode(value)),
            removeItem: key => window.sessionStorage.removeItem(key)
          },
          // storage: window.sessionStorage,
          reducer(val) {
            // console.log('createPersistedState------><><><><', val)
            return {
              // 只储存state中的user,不把page放在这里面
              // user: userStore
              global: val.global,
              index: {
                curApp: val.index.curApp,
                curPermission: val.index.curPermission,
                curPermissionResourceType: val.index.curPermissionResourceType,
                curResourceType: val.index.curResourceType
              },
              login: {
                userInfo: val.login.userInfo
              }
            }
          }
        })
      ]
    })
  }
}

const initStore = store.init(Vue, Vuex)
// 使用这种动态注册的方式有问题,
// initStore.registerModule('index', {
//   ...indexStore,
//   plugins: [myPlugin]
// })

export default initStore

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值