How do I break up my vuex file?

后端 未结 6 758
迷失自我
迷失自我 2020-12-24 08:27

I have a vuex file with a growing mass of mutators, but I\'m not sure of the correct way of splitting it out into different files.

Because I have:

cons

6条回答
  •  滥情空心
    2020-12-24 08:43

    This is another option for splitting your store and have diferent modules, this is already tested.

    structure

    └── src
        ├── assets
        ├── components
        └── store
           └─ modules
              └─ module1
                  ├── state.js
                  ├── actions.js
                  ├── mutations.js
                  ├── getters.js
                  └── index.js
             └─ module2
                  ├── state.js
                  ├── actions.js
                  ├── mutations.js
                  ├── getters.js
                  └── index.js
       └─ index.js
    

    store/index.js

    
    import Vuex from "vuex";
    import thisismodule1 from "./modules/module1";
    import thisismodule2 from "./modules/module2";
    
    const createStore = () => {
      return new Vuex.Store({
        modules: {
          module1: thisismodule1,
          module2: thisismodule2
    
        }
      });
    };
    
    export default createStore;
    

    store/modules/module1/index.js

    import actions from './actions';
    import getters from './getters';
    import mutations from './mutations';
    import state from './state';
    
    
    export default {
      state,
      actions,
      mutations,
      getters
    }
    
    

    store/modules/module2/index.js

    import actions from './actions';
    import getters from './getters';
    import mutations from './mutations';
    import state from './state';
    
    
    export default {
      state,
      actions,
      mutations,
      getters
    }
    
    

    Tipp: Don't forget to import your store to your main.js

提交回复
热议问题