H女王范儿 2019-11-03
本文首发: https://shudong.wang/10415.html
当在vuex的modules全部需要加载的时候
rematch的 models 需要全部加载的时候

每个再需要手动引入到 状态管理里面,我们可以做下面的优化处理
/**
* @author stark.wang
* @blog http://shudong.wang
* The file enables `models` to import all models
* in a one-shot manner. There should not be any reason to edit this file.
*/
const files = require.context('./models', false, /\.js$/)
const models = {}
files.keys().forEach(key => {
const filename = key.replace(/(\.\/|\.js)/g, '')
models[filename] = files(key)['default']
})
export default modelsimport models from './loader'
init({
plugins: [loadingPlugin],
models
})import Vue from 'vue'
import Vuex from 'vuex'
import modules from './loader'
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
modules,
strict: debug
})