Fork me on GitHub

vuex 分模块后使用mapActions调用action老是提示 [vuex] unknown action type:*** 异常

 

vuex 分模块后使用mapActions调用action老是提示 [vuex] unknown action type:*** 异常

 

解决办法:

修改 src/storeindex.js为下面的内容

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)

// 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
}, {})

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

export default store

 

posted @ 2021-08-31 00:10  Hui_Li  阅读(894)  评论(0编辑  收藏  举报