webpack引入所有模块

import { createStore } from 'vuex'
import getters from './getters'

// 自动引入
const modules = {}
const requireStore = require.context('./modules', true, /\.js$/)
requireStore.keys().forEach((fileName) => {
  const file = requireStore(fileName)
  const key = getFileName(fileName)
  modules[key] = file.default || file
})
// 获取文件名
function getFileName (fileName) {
  const startIndex = fileName.lastIndexOf('/')
  const endIndex = fileName.lastIndexOf('.')
  return fileName.substring(startIndex + 1, endIndex)
}

export default createStore({
  modules,
  getters
})

 

posted @ 2022-01-08 16:10  王小美丶  阅读(115)  评论(0编辑  收藏  举报