Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

遍历 vuex-store/modules 下的所有文件,自动加载所有的 vuex 的module #339

Open
ly525 opened this issue Sep 17, 2019 · 0 comments
Assignees

Comments

@ly525
Copy link
Owner

ly525 commented Sep 17, 2019

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import actions from './actions.js'
import mutations from './mutations.js'

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 './loading.js' => 'loading'
  // set './work' => 'work'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.Store({
  modules,
  getters,
  actions,
  mutations
})

export default store
@ly525 ly525 changed the title 便利 vuex-store/modules 下的所有文件,自动加载所有的 vuex 的module 遍历 vuex-store/modules 下的所有文件,自动加载所有的 vuex 的module Sep 17, 2019
@ly525 ly525 self-assigned this Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant