index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* eslint-disable no-undef */
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. import getters from './getters'
  5. import createPersistedState from 'vuex-persistedstate'
  6. Vue.use(Vuex)
  7. // https://webpack.js.org/guides/dependency-management/#requirecontext
  8. const modulesFiles = require.context('./modules', true, /\.js$/)
  9. // you do not need `import app from './modules/app'`
  10. // it will auto require all vuex module from modules file
  11. const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  12. // set './app.js' => 'app'
  13. const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  14. const value = modulesFiles(modulePath)
  15. modules[moduleName] = value.default
  16. return modules
  17. }, {})
  18. const vuexPersisted = createPersistedState({
  19. storage: window.sessionStorage,
  20. key: 'yunti',
  21. reducer(state) {
  22. // 配置需要持久化的数据,有需要在此添加
  23. return {
  24. global: {
  25. cache_keys: state.global.cache_keys
  26. }
  27. }
  28. }
  29. })
  30. const store = new Vuex.Store({
  31. modules,
  32. getters,
  33. plugins: [vuexPersisted]
  34. })
  35. export default store