代码结构:

vue 项目中的 vuex 简单封装_数据

 index.js :

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import state from './state'
import actions from './actions'
import mutations from './mutations'
import modA from './modules/modA.js'

export default new Vuex.Store({
state,
actions,
mutations,
modules: {
modA
}
})

 state.js :

const state = {
count:1
};

export default

mutations.js :

const mutations = {
increment(state,param){
state.count += n
}
}

export default

actions.js :

const actions = {
incrementAct({commit},argNum){ //argNum为参数
commit('increment',augNum)
}
}

export default

modA.js :

// A 模块的内容
const state = {
//A模块的数据
};

const mutations = {
//A模块的 mutations
};

const actions = {
//A模块的actions
};

export default {
state,
actions,
mutations
};

 

详细用法参见官网:​Vuex 官网

​​