[PanJiaChen/vue-admin-template]项目引入 vuex-persistedstate 后,打开标签页空白并且报错

2023-12-17 326 views
4
代码修改处

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)

// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.Store({
  modules,
  getters,
  plugins: [createPersistedState({ storage: window.sessionStorage })]
})

export default store
页面效果与报错

刚改完代码,前几次打开的页面可能还是正常的,但是重启项目或者多尝试打开几个页面,控制台就会报出以下的错误,而且页面空白:

vue-elemenet-admin bug001

还请作者和各位大佬,帮忙指出错误和提供意见,在此感谢了~~

回答

4

我也这样,你解决了吗?

5

{ key: 'VuexPersisted', storage: window.localStorage, reducer: state => ({ user: { name: state.user.name } }) } 我是这样,只缓存部分可用

0
new Vuex.Store({
  modules: {
    app,
    permission,
    settings,
    user,
  },
  getters,
  plugins: [
    createPersistedState({
      key: 'whatever',
      paths: ['user.name']
    })
  ]
})

也可以使用 paths 参数指定需要缓存的状态。