1. 程式人生 > >vuex模組開發

vuex模組開發

  1. 使用vuex模組開發
cnpm i vuex -D
import vuex from "vuex"
Vue.use("vuex")
  1. main.js
import store form './store'
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
  1. src/store
import Vue from "vue";
import Vuex from 'vuex';
import a from './modules/a';
Vue.use(Vuex)

let store = new Vuex.Store({
    modules:{
        a:a
    }
})
export default store
  1. src/store/modules/a
const a = {
    state: {
        msg: "hahah"
    },
    mutations: {
        newMsg(state, msg) {
            state.msg = msg
        }
    },
    actions: {
        newMsg(context, msg) {
            context.commit("newMsg", msg)
        }
    }
}
export default a