How do I break up my vuex file?

后端 未结 6 759
迷失自我
迷失自我 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 09:01

    i have this structure:

    └── store
              └─ module1
                  ├── actions.js
                  ├── mutations.js
                  ├── getters.js
                  └── index.js
       index.js 
    

    and you can copy this example code : index.js from module1

        import actions from "./actions";
        import getters from "./getters";
        import mutations from "./mutations";
        
        import { state } from "./state";
    
        export default {
         state: () => state,
         actions,
         mutations,
         getters,
       };
    

    state:

    export let state = {
      themes: [],
    };
    

    getters:

    const themesList = (state) => {
      return state.themes;
    };
    

    actions:

    const getThemesList = async ({ commit }) => {
      
      commit(types.GET_THEMES, [values]);
    };
    

    mutations:

    const GET_THEMES = (state, themes) => {
      state.themes = themes;
    };
    

    and in the main index.js in store put this

    const createStore = () => {
      return new Vuex.Store({
        modules: {
          module1: module1,
        },
      });
    };
    
    export default createStore;
    

提交回复
热议问题