1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| ... const modulesFiles = require.context('./modules', true, /\.js$/) const _initState = {}
const modules = modulesFiles.keys().reduce((modules, modulePath) => { const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default
_initState[moduleName] = deepClone(value.default.state)
return modules }, {})
const store = new Vuex.Store({ state: { _initState }, modules, getters, mutations: { RESET_ALL_STATE(state, payload) { const initState = deepClone(state._initState) Object.keys(state).forEach(item => { if (item !== '_initState') { state[item] = initState[item] } }) } } }) ...
... const state = {}
const mutations = {}
const actions = { ... logout({ commit, state, dispatch }) { return new Promise((resolve, reject) => { ... commit('RESET_ALL_STATE', null, { root: true }) resolve() }) }, ... }
export default { namespaced: true, state, mutations, actions }
|