Skip to content

Instantly share code, notes, and snippets.

@framon
Created May 10, 2020 18:47
Show Gist options
  • Save framon/f1161c4b63ed09f8a9880e423e57fcf2 to your computer and use it in GitHub Desktop.
Save framon/f1161c4b63ed09f8a9880e423e57fcf2 to your computer and use it in GitHub Desktop.
Vuex: Importar automaticamente todos os módulos do diretório
import Vue from "vue";
import Vuex from "vuex";
import createLogger from 'vuex/dist/logger';
Vue.use(Vuex);
const debug = process.env.NODE_ENV !== "production";
/**
* Automatically imports all the modules and exports as a single module object
*/
const requireModule = require.context('.', false, /\.store\.js$/);
const modules = {};
requireModule.keys().forEach(filename => {
/*
* create the module name from fileName
* remove './' and '.store.js' extension
*/
const moduleName = filename.replace(/(\.\/|\.store\.js)/g, '');
modules[moduleName] = requireModule(filename).default
|| requireModule(filename);
});
export default new Vuex.Store({
modules,
actions: {
RESET({commit}) {
/* resets state of all the modules */
Object.keys(modules).forEach(moduleName => {
commit(`${moduleName}/RESET`);
})
}
},
strict: debug,
plugins: debug ? [createLogger()] : [],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment